I have 2 tables
| Categories | | Products |
:--------------------: :------------------------:
| Id int | | Id int |
| Name nvarchar(max) | | Name vnarchar(max) |
| ApprovedForRelease bit |
| ApprovedForRecall bit |
| CategoryId int |
I need to get the counts for products approvedForRealease, not approvedForRelease, ApprovedForRecall and not ApprovedForRecall for all Categories
Something like this
| Category | #Released | #NotReleased | #Recalled | #NotRecalled |
:----------------------------------------------------------------:
| Arts | 5 | 1 | 3 | 4 |
| Crafts | 13 | 7 | 7 | 8 |
My query looks like this
SELECT
Category = cat.Name,
#Releases = Count(released.Id),
#NotReleased = Count(notReleased.Id),
#Recalled = Count(recalled.Id),
#NotRecalled = Count(notRecalled.Id),
-- Selected product ids
releasedIds = STRING_AGG(released.Id, ', '),
notReleasedIds = STRING_AGG(notReleased.Id, ', '),
recalledIds = STRING_AGG(recalled.Id, ', '),
notRecalledIds = STRING_AGG(notRecalled.Id, ', ')
FROM
Categories as cat
LEFT JOIN Products as released ON released.CategoryId = cat.Id AND released.ApprovedForRelease = 1
LEFT JOIN Products as notReleased ON released.CategoryId = cat.Id AND notReleased.ApprovedForRelease = 0
LEFT JOIN Products as recalled ON released.CategoryId = cat.Id AND recalled.ApprovedForRecall = 1
LEFT JOIN Products as notRecalled ON released.CategoryId = cat.Id AND notRecalled.ApprovedForRecall = 0
GROUP BY
cat.Name
I noticed that product counts are a little too much, so I added the Selected product ids columns to check what actually gets joined and noticed that the joined tables have the same rows multiple times
Example of the result I would get:
| Category | #Released | #NotReleased | #Recalled | #NotRecalled | releasedIds | notReleasedIds | recalledIds | notRecalledIds |
:------------------------------------------------------------------------------------------------------------------------------:
| Arts | 3 | 3 | 3 | 3 | 1, 2, 3 | 4, 5, 6 | 10, 10, 10 | 6, 6, 6 |
| Crafts | 2 | 2 | 4 | 2 | 25, 26 | 96, 98 | 7, 8, 7, 8 | 9, 9 |
Could somebody explain to me what's happening and why do some products get joined multiple times?
And Is there a way to achieve my desired result without using subqueries like:
SELECT
Category = cat.Name,
#Releases = (SELECT COUNT (Id) FROM PRODUCTS WHERE CategoryId = cat.Id AND ApprovedForRelease = 1),
#NotReleased = (SELECT COUNT (Id) FROM PRODUCTS WHERE CategoryId = cat.Id AND ApprovedForRelease = 0),
#Recalled = (SELECT COUNT (Id) FROM PRODUCTS WHERE CategoryId = cat.Id AND ApprovedForRecall= 1),
#NotRecalled = (SELECT COUNT (Id) FROM PRODUCTS WHERE CategoryId = cat.Id AND ApprovedForRecall= 0)
FROM Categories
Use conditional aggregation:
SELECT c.name as category,
SUM(CASE WHEN p.ApprovedForRelease = 1 THEN 1 ELSE 0 END) as released,
SUM(CASE WHEN p.ApprovedForRelease = 0 THEN 1 ELSE 0 END) as not_released,
SUM(CASE WHEN p.ApprovedForRecall = 1 THEN 1 ELSE 0 END) as recalled,
SUM(CASE WHEN p.ApprovedForRecall = 0 THEN 1 ELSE 0 END) as not_recalled
FROM Categories c LEFT JOIN
Products p
ON p.CategoryId = cat.Id
released.ApprovedForRelease = 1
GROUP BY c.name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.