我有以下兩個查詢,我們可以通過該查詢得到每月沒有發生的嚴重致命事故。在一個查詢中獲得結果的SQL查詢優化
如何針對不同的事故類型優化和獲得結果?
SELECT
COUNT(ICT.ID) NoOfAccident,
YEAR(ICT.[Date]) AccidentYear,
Month(ICT.[Date]) AccidentMonth,
MAX(ICT.[Date]) AS AccidentDate
FROM
Accidents ICT
Where
ICT.AccidentType = "Serious"
AND
ICT.[Date] > CONVERT(DATETIME, '09/20/13', 1)
Group By
YEAR(ICT.[Date]),
Month(ICT.[Date])
ORDER BY
IncidentDate ASC
SELECT
COUNT(ICT.ID) NoOfAccident,
YEAR(ICT.[Date]) AccidentYear,
Month(ICT.[Date]) AccidentMonth,
MAX(ICT.[Date]) AS AccidentDate
FROM
Accidents ICT
Where
ICT.AccidentType = "Fatal"
AND
ICT.[Date] > CONVERT(DATETIME, '09/20/13', 1)
Group By
YEAR(ICT.[Date]),
Month(ICT.[Date])
ORDER BY
IncidentDate ASC
我們如何能夠優化並獲得導致像一個查詢:
NoOfSeriousAccident
NoOfFatalAccident
AccidentYear
AccidentMonth
AccidentDate
添加更多我的問題,我們必須得結果如下NoOfSeriousAccident NoOfFatalAccident AccidentYear AccidentMonth AccidentDate –
我已根據您的編輯更新了我的答案。 –