2014-03-26 135 views
0

我有以下兩個查詢,我們可以通過該查詢得到每月沒有發生的嚴重致命事故。在一個查詢中獲得結果的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 

回答

0
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) 
+0

添加更多我的問題,我們必須得結果如下NoOfSeriousAccident NoOfFatalAccident AccidentYear AccidentMonth AccidentDate –

+0

我已根據您的編輯更新了我的答案。 –

1

微不足道的 - 不僅按年份和月份,但也AccidentType組(並刪除每查詢一個accidedenttype的過濾器) 。

您每年/每月獲得2行 - 每個事故類型一個。

0
;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') 
+0

嗨磨憨,查詢結果應該是這樣的「NoOfSeriousAccident NoOfFatalAccident AccidentYear AccidentMonth AccidentDate」 –

+0

耶然後從CTE查詢AccidentType是在條件 – mohan111

+0

嗨磨憨,我得到這個錯誤,當我執行的內層查詢消息156,級別15,狀態1,行20 關鍵字'UNION'附近的語法不正確。 –