问题
What I want to do is basically:
select Type, (Count(*) where Date='')as 10/1, (Count(*) where Date='')as 10/2
from my table
group by Type
What I want it to look like is:
Type 10/1 10/2
1 5 7
2 3 1
3 6 9
4 1 3
5 9 8
However, when I try to run a full select within each count column, I end up getting
Type 10/1 10/2
1 12 15
2 12 15
3 12 15
4 12 15
5 12 15
Any suggestions are appreciated. I'm not sure if I will need to run a pivot or not, but I wouldn't think so. Additionally after I can run that for any specific day, I was thinking about trying to put the date into a variable and trying to run the whole thing for a date range, generating columns dynamically for each day its run. I would probably create a new question for that though.
回答1:
Try this;
SELECT TYPE
,SUM(CASE WHEN MyDate = '' THEN 1 ELSE 0 END) AS [10/1]
,SUM(CASE WHEN MyDate = '' THEN 1 ELSE 0 END) AS [10/2]
FROM MyTable
GROUP BY TYPE
来源:https://stackoverflow.com/questions/21195777/counting-items-rows-in-db-as-columns-grouped-by-another-column