SQL Server按条件搜索

时间:2018-09-28 16:58:08

标签: sql sql-server

具有此结果集

enter image description here

什么逻辑只有在满足以下条件时才有助于结果出现在什么地方?

如果用户传递了TagID 3,则会显示LowLow, Medium

如果用户输入了TagID 4,则仅显示Low, Medium

如果用户输入的TagID为3和4,则仅显示Low, Medium

到目前为止,我已经掌握了这个,但是我在为最后一句话的逻辑而苦苦挣扎

    @List AS dbo.ReportingItemIDList READONLY
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT RI.ID, ItemContent,
    FROM dbo.ReportingItem RI
    JOIN dbo.ItemTag IT ON IT.ReportingItemID = RI.ID
    JOIN @List ON IT.TagID = [@List].ID 

任何建议(不需要这样回答)都是有用的

1 个答案:

答案 0 :(得分:1)

因此,您希望所有标签共用itemscontent

select itemscontent
from resultset
where tagid in (3, 4)
group by itemscontent
having count(*) = 2;  -- 2 is the number of tags