Answered
CASE statement in SQL Server
I am working on a project for ambulance response times. In
the following query which is in my coldfusion code, I am using a
CASE statement on a subquery to count the ambulance response times
in bins. An ambulance should arrive at an emergency incident in
less than 8:59 (539 seconds) or else it is considered late. In my
coldfusion Transact-SQL code I am:
1.) doing a subquery.
2.) counting the 'event numbers' based on the time it took for the ambulance to arrive.
3.) only counting Lee County ambulances and excluding A6 type calls (non-emergencies).
4.) grouping it by the dateparts.
SELECT DATENAME("M", I.I_tTimeDispatch) as mths, (DATEPART("yyyy", I.I_tTimeDispatch)) AS yr,
COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch, I.I_tTimeArrival)) BETWEEN 0 AND 539 THEN evnt END) AS OnTime,
COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch, I.I_tTimeArrival)) BETWEEN 540 AND 1028 THEN evnt END) AS Late,
COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch, I.I_tTimeArrival)) > 1028 THEN evnt END) AS Outlier
FROM (SELECT I_EventNumber AS evnt, I_tTimeDispatch, I_tTimeArrival, I_kTypeInfo, I_Agency FROM dbo.IIncident) as I
INNER JOIN dbo.ITypeInfo AS T ON I.I_kTypeInfo = T.ITI_TypeInfo_PK
WHERE I.I_Agency='LC'
AND T.ITI_TypeID NOT LIKE 'A6*'
GROUP BY (DATEPART("M", I.I_tTimeDispatch)), (DATENAME("M", I.I_tTimeDispatch)), (DATEPART("yyyy", I.I_tTimeDispatch))
ORDER BY (DATEPART("yyyy", I.I_tTimeDispatch)) ASC, (DATEPART("M", I.I_tTimeDispatch)) ASC
Here is my problem!
I go into Microsoft Access to verify my statistics and I get different counts. For instance, in April 2008 my coldfusion query returns 3,944 on-time ambulance responses. My Access query for the same time period using only Lee County ambulances and excluding A6 non-emergencies returns only 3,805 responses. This is an undercount of 139 responses. Even for my other time bins I am getting an undercount.
Here is my Access SQL for the on time response bin (<539 seconds or 8:59):
SELECT Count(dbo_IIncident.I_EventNumber) AS CountOfI_EventNumber
FROM dbo_IIncident INNER JOIN dbo_ITypeInfo ON dbo_IIncident.I_kTypeInfo = dbo_ITypeInfo.ITI_TypeInfo_PK
WHERE (((dbo_IIncident.I_Agency)="lc") AND ((dbo_ITypeInfo.ITI_TypeID) Not Like "a6*") AND ((dbo_IIncident.I_tTimeDispatch) Between #4/1/2008# And #5/1/2008#) AND ((DateDiff("s",[dbo_IIncident]![I_tTimeDispatch],[dbo_IIncident]![I_tTimeArrival])) Between 0 And 539));
How could two queries that are supposed to be doing the same thing return such different results?
To clear up any confusion I am temporarily posting the page. Please look at it because it may help you visualize the problem.
http://lcfcfn01/Secure/GTandLT_8_59.cfm
1.) doing a subquery.
2.) counting the 'event numbers' based on the time it took for the ambulance to arrive.
3.) only counting Lee County ambulances and excluding A6 type calls (non-emergencies).
4.) grouping it by the dateparts.
SELECT DATENAME("M", I.I_tTimeDispatch) as mths, (DATEPART("yyyy", I.I_tTimeDispatch)) AS yr,
COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch, I.I_tTimeArrival)) BETWEEN 0 AND 539 THEN evnt END) AS OnTime,
COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch, I.I_tTimeArrival)) BETWEEN 540 AND 1028 THEN evnt END) AS Late,
COUNT(CASE WHEN (DATEDIFF("S",I.I_tTimeDispatch, I.I_tTimeArrival)) > 1028 THEN evnt END) AS Outlier
FROM (SELECT I_EventNumber AS evnt, I_tTimeDispatch, I_tTimeArrival, I_kTypeInfo, I_Agency FROM dbo.IIncident) as I
INNER JOIN dbo.ITypeInfo AS T ON I.I_kTypeInfo = T.ITI_TypeInfo_PK
WHERE I.I_Agency='LC'
AND T.ITI_TypeID NOT LIKE 'A6*'
GROUP BY (DATEPART("M", I.I_tTimeDispatch)), (DATENAME("M", I.I_tTimeDispatch)), (DATEPART("yyyy", I.I_tTimeDispatch))
ORDER BY (DATEPART("yyyy", I.I_tTimeDispatch)) ASC, (DATEPART("M", I.I_tTimeDispatch)) ASC
Here is my problem!
I go into Microsoft Access to verify my statistics and I get different counts. For instance, in April 2008 my coldfusion query returns 3,944 on-time ambulance responses. My Access query for the same time period using only Lee County ambulances and excluding A6 non-emergencies returns only 3,805 responses. This is an undercount of 139 responses. Even for my other time bins I am getting an undercount.
Here is my Access SQL for the on time response bin (<539 seconds or 8:59):
SELECT Count(dbo_IIncident.I_EventNumber) AS CountOfI_EventNumber
FROM dbo_IIncident INNER JOIN dbo_ITypeInfo ON dbo_IIncident.I_kTypeInfo = dbo_ITypeInfo.ITI_TypeInfo_PK
WHERE (((dbo_IIncident.I_Agency)="lc") AND ((dbo_ITypeInfo.ITI_TypeID) Not Like "a6*") AND ((dbo_IIncident.I_tTimeDispatch) Between #4/1/2008# And #5/1/2008#) AND ((DateDiff("s",[dbo_IIncident]![I_tTimeDispatch],[dbo_IIncident]![I_tTimeArrival])) Between 0 And 539));
How could two queries that are supposed to be doing the same thing return such different results?
To clear up any confusion I am temporarily posting the page. Please look at it because it may help you visualize the problem.
http://lcfcfn01/Secure/GTandLT_8_59.cfm
