SQL get rows in '= ALL' style

烈酒焚心 提交于 2019-12-06 02:23:13
select PC.ProductId
from #Product_Category as PC
where PC.CategoryId in (200, 300, 400)
group by PC.ProductId
having count(distinct PC.CategoryId) = 3

Update: It is still ugly but it does work:

SELECT DISTINCT master.ProductId
FROM #Product_Category master
JOIN (
    SELECT ProductId, 
           cat200 = max(case when CategoryId=200 then 1 else 0 end),
           cat300 = max(case when CategoryId=300 then 1 else 0 end),
           cat400 = max(case when CategoryId=400 then 1 else 0 end)
   FROM #Product_Category
   GROUP BY ProductId
) sub ON sub.ProductId = master.ProductId
WHERE cat200=1
  and cat300=1
  AND cat400=1

try this

SELECT a.ProductId 
    FROM Product_Category as a, 
        Product_Category as b, 
        Product_Category as c 
        WHERE a.CategoryId = 200 
            And b.`CategoryId` = 300 
            And c.`CategoryId` = 400 
            And a.`ProductId` = b.`ProductId` 
            And b.`ProductId` = c.`ProductId`

for more like 500 and 600

SELECT a.ProductId 
    FROM Product_Category as a, 
        Product_Category as b, 
        Product_Category as c,
        Product_Category as d,
        Product_Category as e,
        WHERE a.CategoryId = 200 
            And b.`CategoryId` = 300 
            And c.`CategoryId` = 400 
            And d.`CategoryId` = 500 
            And e.`CategoryId` = 600 
            And a.`ProductId` = b.`ProductId` 
            And b.`ProductId` = c.`ProductId`
            And c.`ProductId` = d.`ProductId`
            And d.`ProductId` = e.`ProductId`

check live demo http://sqlfiddle.com/#!2/8965e/1/0

Here I am using a CTE, but you could use a table variable or something different for the category_filter.

with category_filter as (
    select * from (values (200), (300), (400)) as v(id)
)
select distinct ProductId
from #Product_Category
join category_filter
    on (#Product_Category.CategoryId = category_filter.id)
group by ProductId
having COUNT(distinct CategoryId) = (select COUNT(*) from category_filter)

Also ugly solution :)

WITH category_filter1(CategoryId) AS (
    SELECT * FROM (VALUES (200), (300), (400)) tmp1(tmp2)
)
SELECT p.ProductId
FROM (
    SELECT ProductId,
        CASE WHEN CategoryId IN (SELECT CategoryId FROM category_filter1) THEN 1 ELSE 0 END f
    FROM #Product_Category
    ) p
GROUP BY p.ProductId, p.f
HAVING COUNT(*) = (SELECT COUNT(*) FROM category_filter1);

You can use Values as Table Source and check them in WHERE clause with NOT EXISTS and EXCEPT operators

SELECT *
FROM #Product_Category p
WHERE NOT EXISTS (                                                        
                  SELECT Match
                  FROM (VALUES(200), 
                              (300),
                              (400))
                  x(Match)
                  EXCEPT
                  SELECT CategoryId
                  FROM #Product_Category p2
                  WHERE p.ProductID = p2.ProductID
                  )  

Demo on SQLFiddle

WITH L AS (
SELECT *
  FROM (VALUES (200),(300),(400)) AS T(CategoryId)
)
SELECT ProductId
  FROM Product_Category P
 INNER JOIN L
    ON L.CategoryId = P.CategoryId
 GROUP BY ProductId
HAVING COUNT(1) = (SELECT Count(1) FROM L)
;

The WITH disappears if you are planning to use TVP.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!