我想分别计算每年的事故总数和每年发生的车祸数量。然而,我的代码似乎只计算事故总数。
到目前为止,我的代码是:
Select Year, Count(*) AS 'Total', Count(*) AS 'Car'
FROM ACCIDENTS
WHERE Car = 1
GROUP BY Year, Car
Order BY Year ASC;
表应该是这样的:
Year | Accidents | Car
1990 500 25
1991 521 18
表格结构:
我会计算事故总数的年数,然后事故类型会有布尔值来表示是否是那种事故。
答案 0 :(得分:1)
使用以下查询
Select Year, Count(*) AS 'Total', count(case when car =1 then 1 else null end) FROM ACCIDENTS GROUP BY Year Order BY Year ASC;