所有行都标记有一组特定的值

时间:2014-07-17 21:28:51

标签: sql sql-server sql-server-2008 sql-server-2012

更具体地说,对于问题和标签之间的多对多关系作为sql server表(包括帮助表QuestionsWithTags),需要一个query / sp,它返回所有具有以下标记集“t1”的问题,“t2”和“t3”。 鉴别器没有任何标签,但所有这些标签都不少于此。

提前致谢。

1 个答案:

答案 0 :(得分:6)

您可以使用聚合和having子句来解决此问题:

select questionid
from QuestionsWithTags qwt
group by questionid
having count(distinct tag) = 3 and
       count(distinct case when tag in ('t1', 't2', 't3') then tag end) = 3;