[英]SQL query optimization to get result in one query
通過下面兩個查詢,我每個月都不會遇到嚴重的致命事故。
如何在一個查詢中針對不同的事故類型優化並獲得結果?
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
平凡的-不僅按年份和月份分組,還按AccidentType分組(並刪除每個查詢一種accidedenttype的過濾器)。
這樣,您每年/每月將獲得2行-每種事故類型一次。
SELECT
ICT.AccidentType,
COUNT(ICT.ID) NoOfAccident,
YEAR(ICT.[Date]) AccidentYear,
Month(ICT.[Date]) AccidentMonth,
MAX(ICT.[Date]) AS AccidentDate
FROM
Accidents ICT
Where
ICT.AccidentType IN ("Serious","Fatal")
AND
ICT.[Date] > CONVERT(DATETIME, '09/20/13', 1)
Group By
ICT.AccidentType
YEAR(ICT.[Date]),
Month(ICT.[Date])
ORDER BY
IncidentDate ASC
編輯:根據更新的要求,您可以使用PIVOT來獲取致命和嚴重事故計數的單獨列,如下所示:
;with pivoted as
(select
accidentyear,
accidentmonth,
serious as NoOfSeriousAccident,
fatal as NoOfFatalAccident from
(SELECT
ICT.AccidentType,
COUNT(ICT.ID) cnt,
YEAR(ICT.[accidentdate]) AccidentYear,
Month(ICT.[accidentdate]) AccidentMonth
FROM
Accident ICT
Where
ICT.AccidentType IN ('Serious','Fatal')
AND
ICT.[accidentdate] > CONVERT(DATETIME, '09/20/13', 1)
Group By
ICT.AccidentType,
YEAR(ICT.[accidentdate]),
Month(ICT.[accidentdate])) as s
pivot
(
max(cnt)
for accidenttype in ([serious] ,[fatal])
) as pvt
)
select
x.accidentyear,
x.accidentmonth,
max(a.accidentdate),
x.NoOfSeriousAccident,
x.NoOfFatalAccident,
from pivoted x
inner join accident a
on month(a.accidentdate) = x.accidentmonth
and year(a.accidentdate) = x.accidentyear
group by x.accidentmonth, x.accidentyear, x.seriouscount, x.fatalcount
order by max(a.accidentdate)
;WITH CTE AS (
SELECT
COUNT(ICT.ID) NoOfAccident,
YEAR(ICT.[Date]) AccidentYear,
Month(ICT.[Date]) AccidentMonth,
MAX(ICT.[Date]) AS AccidentDate,
ICT.AccidentType As AccidentType
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
UNION ALL
SELECT
COUNT(ICT.ID) NoOfAccident,
YEAR(ICT.[Date]) AccidentYear,
Month(ICT.[Date]) AccidentMonth,
MAX(ICT.[Date]) AS AccidentDate,
ICT.AccidentType As AccidentType
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
)
select NoOfAccident,AccidentYear,AccidentMonth,AccidentDate,AccidentType from CTE
WHERE AccidentType IN ('Fatal','Serious')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.