Solved

MS Query Case When syntax error

Posted on 2015-01-24
4
115 Views
Last Modified: 2015-01-24
Can someone correct the syntax of the following MS Query SQL?  There is an error in the case when statement to sum the SIP field.  The SIP field is a Y/N field.

SELECT

tbl_NHSN_SSI_Pts.SurgeonID,
tbl_NHSN_SSI_Pts.Surgeon,  
Count(tbl_NHSN_SSI_Pts.SurgeonID) AS 'SURG',

sum(CASE WHEN tbl_NHSN_SSI_Pts.SIP = 'Yes' then 1 else 0 end ) as 'INF'

FROM `P:\IC-Employee Health\SSI\NHSN.accdb`.tbl_NHSN_SSI_Pts tbl_NHSN_SSI_Pts



GROUP BY tbl_NHSN_SSI_Pts.Surgeon, tbl_NHSN_SSI_Pts.SurgeonID
ORDER BY tbl_NHSN_SSI_Pts.Surgeon, tbl_NHSN_SSI_Pts.SurgeonID


Thanks

Glen
0
Comment
Question by:GPSPOW
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40568424
By Yes/No field do you mean a bit field or a character field that stores "Yes" and "No". If the latter then your case statement should work.
0
 

Author Comment

by:GPSPOW
ID: 40568429
Yes.
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40568432
For a bit datatype field, use this version of the CASE syntax

sum(CASE tbl_NHSN_SSI_Pts.SIP when 1 then 1 else 0 end ) as 'INF'
0
 

Author Closing Comment

by:GPSPOW
ID: 40568447
Thanks

I also found that if I change it to a MS-Access  IIF clause this works too.

Glen
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now