Select Name,
sum(case when DayAci = 'Sunday' then 1 else 0 END) as Sunday,
sum(case when DayAci = 'Monday' then 1 else 0 END) as Monday,
sum(case when DayAci = 'Tuesday' then 1 else 0 END) as Tuesday,
sum(case when DayAci = 'Wednesday' then 1 else 0 END) as Wednesday,
sum(case when DayAci = 'Thursday' then 1 else 0 END) as Thursday,
sum(case when DayAci = 'Friday' then 1 else 0 END) as Friday,
sum(case when DayAci = 'Saturday' then 1 else 0 END) as Saturday,
count(*) as Total
from Caraccident
where Accident = 'Near-miss'
group by Name;
Select Name,
count(*) as Total
From CaraccidentPrevious
where Accident = 'Near-miss'
group by Name;
要在表格中這樣如何將這兩個sql查詢合併爲一個?
Name | Sunday | Monday | Tuesday | ..... | Total | Previous Total
Joe 0 2 1 3 5
顯示信息的第一個SQL語句給我我需要的每一天,總的數據。 第二個給我我以前總計需要的數據。使用SQL Server
感謝您的幫助再次 –
這應該是一個'COALESCE(MAX(cap.prevTotal) 0)AS prevTotal'來防止由於LEFT JOIN導致的NULL計數。 – Curt