我有以下表格:
Table @t1 (Specific Person brnaches)
BranchID
10
11
Table @t2
BranchID Holiday
10 2018-01-01
12 2018-01-01
10 2018-01-05
11 2018-01-05
Table @t3 (Holidays)
Holiday
2018-01-01
2018-01-02
2018-01-03
2018-01-04
2018-01-05
declare @t1 table
(
BranchID int
)
declare @t2 table
(
BranchID int,
Holiday date
)
declare @t3 table
(
Holiday date
)
insert into @t1
values(10),(11)
insert into @t2
values (10,'2018-01-01'),(12,'2018-01-01'),(10,'2018-01-05'),(11,'2018-01-05')
insert into @t3
values ('2018-01-01'),('2018-01-02'),('2018-01-03'),('2018-01-04'),('2018-01-05')
来自@ t3的结果必须是' 2018-01-05' 因为表格@ t2中的10,11 BranchID在' 2018-01-05'在假日
如何阻止它?
答案 0 :(得分:3)
我觉得你不需要在这里使用表格@T3
,你可以尝试按照以下方式获得所需的输出。
SELECT DISTINCT Holiday FROM
(
SELECT *, COUNT(*) OVER(PARTITION BY Holiday ORDER BY Holiday) AS HC
FROM @t2 BH
WHERE EXISTS
(
SELECT 1 FROM @T1 T1 WHERE T1.BranchID=BH.BranchID
-- Put condition like AND PersonId=@PersonId to fetch all the branches of the user.
)
) T
WHERE T.HC = (SELECT COUNT(*) FROM @T1) --Total branch count for a person
<强>输出强>
+------------+
| Holiday |
+------------+
| 2018-01-05 |
+------------+