TSQL如何使where子句匹配所有'IN'多个值

时间:2021-12-01 20:18:20

I am using 4 dates in my where clause using IN. For example

我在使用IN的where子句中使用了4个日期。例如

...where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')

My query will return a result if even one date matches but I want my where clause to match ALL the dates. I'm sure there has to be an easy solution for this.

如果一个日期匹配,我的查询将返回结果,但我希望我的where子句匹配所有日期。我相信必须有一个简单的解决方案。

1 个解决方案

#1


5  

You want to match your data for a specific column value. Group by that column and take only those groups having all 4 dates

您希望匹配特定列值的数据。按该列分组,仅采用具有所有4个日期的组

select col
from your_table
where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')
group by col
having count(distinct date) = 4

#1


5  

You want to match your data for a specific column value. Group by that column and take only those groups having all 4 dates

您希望匹配特定列值的数据。按该列分组,仅采用具有所有4个日期的组

select col
from your_table
where date in ('2017-01-01', '2017-01-02', '2017-01-03','2017-01-04')
group by col
having count(distinct date) = 4