SQL查询优化以在一个查询中获得结果
我有以下两个问题,通过使用这些问题,我们每月不会发生严重、致命的事故 如何在一次查询中针对不同的事故类型进行优化并获得结果SQL查询优化以在一个查询中获得结果,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有以下两个问题,通过使用这些问题,我们每月不会发生严重、致命的事故 如何在一次查询中针对不同的事故类型进行优化并获得结果 SELECT COUNT(ICT.ID) NoOfAccident, YEAR(ICT.[Date]) AccidentYear, Month(ICT.[Date]) AccidentMonth, MAX(ICT.[Date]) AS AccidentDate FROM Acciden
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分组(并删除每个查询一个AccidentType的筛选器) 然后每年/每月得到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)
为了补充我的问题,我们必须得到如下结果:NoofPeriousAccident NoofTalConference Accident Year Accident Month Accident Hi Mohan,查询结果应该是“NoofPeriousAccident NoofTalConference Accident Year Accident Month Accident Date”,是的,然后从cte查询中删除AccidentType,在Where条件下Hi Mohan,当我在关键字“UNION”附近执行内部查询msg156,级别15,状态1,第20行时,出现了这个错误。
;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')