Skip to main content
Participant
May 30, 2008
Answered

CASE statement in SQL Server

  • May 30, 2008
  • 3 replies
  • 912 views
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
This topic has been closed for replies.
Correct answer paross1
Instead of COUNTing, why don't you modify your SQL to list the incidents in both queries instead, then you can actually see which ones are being included in the T-SQL that are not being captured in Access.

Phil

3 replies

Participant
June 2, 2008
Thank you very much,

Your suggestion to list all incidents in both queries was spot-on. When I output the Transact SQL results and linked them up to the Microsoft Access results the answer was apparent. As it turns out I was using the wrong wildcard. In order to exclude all varieties of 'A6's (interfacility transfers) I was using the Access wildcard of "not like A6*". I should have been using the Transact SQL wildcard of "not like A6%".
It was one character that was skewing my results so badly. I forgot to use % in place of *

thank you,
David
Participant
May 30, 2008
Thank you for your quick reply.
I thought about that, but it isn't what is causing the discrepancy in the numbers. This is because Access is hitting the SQL Server through ODBC. The time stamps in SQL Server are ODBC datetime stamps so they look like this: 4/19/2008 6:20:18 PM

When my query uses the date #5/1/2008# it is like saying May 1, 2008 00:00:00. Please correct me if I am wrong. The query won't return any results from May 1, 2008 because it stops at zero hundred hours. I believe it will only go to April 30, 2008 23:59:59 and then stop there.

I do try and play with the date ranges and the 'seconds' (<539 or >539) parameter and I consistently get different results from what my coldfusion page is telling me.

David
Participating Frequently
May 30, 2008
What happens then if you specify #4/30/2008#?

EDIT: Never mind... you are right....

Phil
paross1Correct answer
Participating Frequently
May 30, 2008
Instead of COUNTing, why don't you modify your SQL to list the incidents in both queries instead, then you can actually see which ones are being included in the T-SQL that are not being captured in Access.

Phil
Participating Frequently
May 30, 2008
One thinng that may be causing this is that in your T-SQL, your are counting the incidents that are less than 539 seconds for the month (grouping by DATEPART("M"...), but in your Access query you are counting the incidents that are less than 539 seconds between #4/1/2008# And #5/1/2008#, which means that your Access query is also including incidents on the first day of the next month (5/1/2008) since BETWEEN includes incidents on BOTH dates inclusive, as well as all dates between.

Phil