Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 combine case statement

Author  Topic 

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-11 : 15:02:36
i would like to combine the red and blue case statement to one
case when u.name or u2.name in ('list of names') then dr

not working for me... getting errors

SELECT
cv.ClientDisplayName AS Patient,
cv.IDCode AS MRN,
cv.VisitIDCode AS VISIT_ID,
cv.guid AS ClientVisitGUID,
cd.GUID AS ClientDocGUID,
CASE WHEN (u.displayname IN ('CAMPBELL, JULIE','JIA, YUXIA','Lee, Sangmin','LEE, YOOMI','LIM, EMERSON',
'Oberstein, Paul','Pan, Beiqing','SCHECTER, JORDAN','SHOHARA, RYO','WU, DAVID','Xu, Xunhai','Zikria, Jennifer Marie')) THEN 'FELLOWS'
when (u2.displayname IN ('CAMPBELL, JULIE','JIA, YUXIA','Lee, Sangmin','LEE, YOOMI','LIM, EMERSON',
'Oberstein, Paul','Pan, Beiqing','SCHECTER, JORDAN','SHOHARA, RYO','WU, DAVID','Xu, Xunhai','Zikria, Jennifer Marie')) THEN 'FELLOWS'
WHEN (u.displayname IN ('Crew, Katherine','Diuguid, David L','EISENBERGER, ANDREW','Fine, Robert L','Flamm, Michael J','Gelmann, Edward',
'Halmos, Balazs','Hershman, Dawn L','Jia, Yuxia','Kalinsky, Kevin','Matushansky, Igor','Maurer, Mathew','Mukherjee, Siddhartha',
'Neugut, Alfred I','OBERSTEIN, PAUL','Petrylak, Daniel P','Phillips, Adrienne','Raza, Azra','SAIF, MOHAMMED','Saif, Muhammad Wasif',
'SAVAGE, DAVID','Savage, David G','Shohara, Ryo','SIEGAL, A'))then 'ATTENDINGS'
WHEN (u2.displayname IN ('Crew, Katherine','Diuguid, David L','EISENBERGER, ANDREW','Fine, Robert L','Flamm, Michael J','Gelmann, Edward',
'Halmos, Balazs','Hershman, Dawn L','Jia, Yuxia','Kalinsky, Kevin','Matushansky, Igor','Maurer, Mathew','Mukherjee, Siddhartha',
'Neugut, Alfred I','OBERSTEIN, PAUL','Petrylak, Daniel P','Phillips, Adrienne','Raza, Azra','SAIF, MOHAMMED','Saif, Muhammad Wasif',
'SAVAGE, DAVID','Savage, David G','Shohara, Ryo','SIEGAL, A')) then 'ATTENDINGS' END AS PHYSICIAN_TYPE,
cd.DocumentName AS NOTE_NAME,
cd.AuthoredDtm AS SERVICE_DATE,
u2.displayname AS AUTHORED_BY,
u.DisplayName AS EnteredBy,
dbo.CV3HealthIssueDeclaration.text AS ADMISSION_DIAGNOSIS

FROM
dbo.CV3ClientDocument AS cd WITH (nolock)
INNER JOIN
dbo.CV3ClientVisit AS cv WITH (nolock) ON cd.ClientVisitGUID = cv.GUID AND cv.TypeCode = 'Inpatient'
INNER JOIN
dbo.CV3User AS u WITH (nolock) ON cd.UserGUID = u.GUID
and u.displayname in ('CAMPBELL, JULIE','Crew, Katherine','Diuguid, David L','Eisenberger, Andrew',
'Fine, Robert L','Flamm, Michael J','Gelmann, Edward','Halmos, Balazs','Hershman, Dawn L','JIA, YUXIA','Kalinsky, Kevin',
'Lee, Sangmin','LEE, YOOMI','Lim, Emerson','Matushansky, Igor','Maurer, Mathew','Mukherjee, Siddhartha','Neugut, Alfred I',
'Oberstein, Paul','Pan, Beiqing','Petrylak, Daniel P','Phillips, Adrienne','Raza, Azra','SAIF, MOHAMMED','Saif, Muhammad Wasif',
'SAVAGE, DAVID','Savage, David G','SCHECTER, JORDAN','SHOHARA, RYO','SIEGAL, A','WU, DAVID','XU, XUNHAI',
'Zikria, Jennifer Marie')
and u.GUID in (select u2.GUID from cv3user u2 where u2.Displayname in ('CAMPBELL, JULIE','Crew, Katherine','Diuguid, David L','Eisenberger, Andrew',
'Fine, Robert L','Flamm, Michael J','Gelmann, Edward','Halmos, Balazs','Hershman, Dawn L','JIA, YUXIA','Kalinsky, Kevin',
'Lee, Sangmin','LEE, YOOMI','Lim, Emerson','Matushansky, Igor','Maurer, Mathew','Mukherjee, Siddhartha','Neugut, Alfred I',
'Oberstein, Paul','Pan, Beiqing','Petrylak, Daniel P','Phillips, Adrienne','Raza, Azra','SAIF, MOHAMMED','Saif, Muhammad Wasif',
'SAVAGE, DAVID','Savage, David G','SCHECTER, JORDAN','SHOHARA, RYO','SIEGAL, A','WU, DAVID','XU, XUNHAI','Zikria, Jennifer Marie'))
INNER JOIN
dbo.CV3Location AS loc WITH (nolock) ON cv.CurrentLocationGUID = loc.GUID and ParentGUID = '4000001145061001'
INNER JOIN
dbo.CV3HealthIssueDeclaration WITH (nolock) ON cv.GUID = dbo.CV3HealthIssueDeclaration.ClientVisitGUID AND
cv.ClientGUID = dbo.CV3HealthIssueDeclaration.ClientGUID AND dbo.CV3HealthIssueDeclaration.TypeCode = 'Admitting Dx'
and dbo.CV3HealthIssueDeclaration.status='active'
INNER JOIN
dbo.CV3User u2 WITH (nolock) ON cd.AuthoredProviderGUID = u2.GUID
where cd.AuthoredDtm between @start_date and @end_date

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-11 : 15:20:40
Why not put the list of names in a variable or something??


Declare @NameList varchar(1000)

Set @NameList = ',''CAMPBELL, JULIE'',''JIA, YUXIA'',''Lee, Sangmin'',''LEE, YOOMI'',''LIM, EMERSON'',''Oberstein, Paul'',''Pan, Beiqing'',
''SCHECTER, JORDAN'',''SHOHARA, RYO'',''WU, DAVID'',''Xu, Xunhai'',''Zikria, Jennifer Marie'','

Select
....,
CASE when @NameList like '%,'''+u.displayname+''',%' THEN 'FELLOWS'
when @NameList like '%,'''+u2.displayname+''',%' THEN 'FELLOWS'
when ....


Corey

I Has Returned!!

edit: reformatted for long line
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-11 : 16:13:09
Or how about adding a Physicians table and add each name and type of physician? Then you can just JOIN to that table and display the type without the long CASE expression. And you don't have to change the query if you get a new doctor, just add them to the table.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2011-04-11 : 16:40:37
quote:
Originally posted by robvolk

Or how about adding a Physicians table and add each name and type of physician? Then you can just JOIN to that table and display the type without the long CASE expression. And you don't have to change the query if you get a new doctor, just add them to the table.



I really agree.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-11 : 17:09:56
quote:
Originally posted by denis_the_thief

quote:
Originally posted by robvolk

Or how about adding a Physicians table and add each name and type of physician? Then you can just JOIN to that table and display the type without the long CASE expression. And you don't have to change the query if you get a new doctor, just add them to the table.



I really agree.



Me really too.

Corey

I Has Returned!!
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-04-11 : 19:35:57
I find more flaws in this design -- why are you using GUIDs? why are almost NONE of the data element names correct? There is no such thing as a generic status. There is no such thing as an "id_code". Data elements change names. Etc.

Let me be blunt; what do you think you did right! I do not see anything.



--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-12 : 09:15:32
Let me be blunt; what do you think you did right! I do not see anything. i didnt design the database and name the fields. the id codes are actual field names.

thank you those who are actually trying to help and not critize. i could create a temp table, but not so sure how to join that back the the query. ill give it a shot. thanks again for the help.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-04-12 : 09:44:56
Something like this:
CREATE TABLE #docs (Name VARCHAR(60) NOT NULL, TYPE VARCHAR(12) NOT NULL)

INSERT #docs(name, TYPE) SELECT 'CAMPBELL, JULIE', 'FELLOW'
UNION ALL SELECT 'JIA, YUXIA', 'FELLOW'
UNION ALL SELECT 'Lee, Sangmin', 'FELLOW'
UNION ALL SELECT 'LEE, YOOMI', 'FELLOW'
UNION ALL SELECT 'LIM, EMERSON', 'FELLOW'
UNION ALL SELECT 'Oberstein, Paul', 'FELLOW'
UNION ALL SELECT 'Pan, Beiqing', 'FELLOW'
UNION ALL SELECT 'SCHECTER, JORDAN', 'FELLOW'
UNION ALL SELECT 'SHOHARA, RYO', 'FELLOW'
UNION ALL SELECT 'WU, DAVID', 'FELLOW'
UNION ALL SELECT 'Xu, Xunhai', 'FELLOW'
UNION ALL SELECT 'Zikria, Jennifer Marie', 'FELLOW'

INSERT #docs(name, TYPE)
SELECT 'Crew, Katherine', 'ATTENDING'
UNION ALL SELECT 'Diuguid, David L', 'ATTENDING'
UNION ALL SELECT 'EISENBERGER, ANDREW', 'ATTENDING'
UNION ALL SELECT 'Fine, Robert L', 'ATTENDING'
UNION ALL SELECT 'Flamm, Michael J', 'ATTENDING'
UNION ALL SELECT 'Gelmann, Edward', 'ATTENDING'
UNION ALL SELECT 'Halmos, Balazs', 'ATTENDING'
UNION ALL SELECT 'Hershman, Dawn L', 'ATTENDING'
UNION ALL SELECT 'Jia, Yuxia', 'ATTENDING'
UNION ALL SELECT 'Kalinsky, Kevin', 'ATTENDING'
UNION ALL SELECT 'Matushansky, Igor', 'ATTENDING'
UNION ALL SELECT 'Maurer, Mathew', 'ATTENDING'
UNION ALL SELECT 'Mukherjee, Siddhartha', 'ATTENDING'
UNION ALL SELECT 'Neugut, Alfred I', 'ATTENDING'
UNION ALL SELECT 'OBERSTEIN, PAUL', 'ATTENDING'
UNION ALL SELECT 'Petrylak, Daniel P', 'ATTENDING'
UNION ALL SELECT 'Phillips, Adrienne', 'ATTENDING'
UNION ALL SELECT 'Raza, Azra', 'ATTENDING'
UNION ALL SELECT 'SAIF, MOHAMMED', 'ATTENDING'
UNION ALL SELECT 'Saif, Muhammad Wasif', 'ATTENDING'
UNION ALL SELECT 'SAVAGE, DAVID', 'ATTENDING'
UNION ALL SELECT 'Savage, David G', 'ATTENDING'
UNION ALL SELECT 'Shohara, Ryo', 'ATTENDING'
UNION ALL SELECT 'SIEGAL, A', 'ATTENDING'


SELECT cv.ClientDisplayName AS Patient,
cv.IDCode AS MRN,
cv.VisitIDCode AS VISIT_ID,
cv.guid AS ClientVisitGUID,
cd.GUID AS ClientDocGUID,
d1.Type AS PHYSICIAN_TYPE,
cd.DocumentName AS NOTE_NAME,
cd.AuthoredDtm AS SERVICE_DATE,
u2.displayname AS AUTHORED_BY,
u.DisplayName AS EnteredBy,
dbo.CV3HealthIssueDeclaration.text AS ADMISSION_DIAGNOSIS
FROM dbo.CV3ClientDocument AS cd WITH (NOLOCK)
INNER JOIN dbo.CV3ClientVisit AS cv WITH (NOLOCK) ON cd.ClientVisitGUID = cv.GUID AND cv.TypeCode = 'Inpatient'
INNER JOIN dbo.CV3User AS u WITH (NOLOCK) ON cd.UserGUID = u.GUID
LEFT JOIN #docs d1 ON u.displayname=d1.Name
LEFT JOIN dbo.CV3User u2 ON u.GUID=u2.GUID AND d1.Name=u2.Displayname

INNER JOIN dbo.CV3Location AS loc WITH (NOLOCK) ON cv.CurrentLocationGUID = loc.GUID AND ParentGUID = '4000001145061001'
INNER JOIN dbo.CV3HealthIssueDeclaration WITH (NOLOCK) ON cv.GUID = dbo.CV3HealthIssueDeclaration.ClientVisitGUID AND
cv.ClientGUID = dbo.CV3HealthIssueDeclaration.ClientGUID AND dbo.CV3HealthIssueDeclaration.TypeCode = 'Admitting Dx'
AND dbo.CV3HealthIssueDeclaration.status='active'
INNER JOIN dbo.CV3User u2 WITH (NOLOCK) ON cd.AuthoredProviderGUID = u2.GUID
WHERE cd.AuthoredDtm BETWEEN @start_date AND @end_date
The JOIN for the temp table is in red. I'm not sure you need the bolded part though, you'll have to explain a bit what the GUID is supposed to do and why you need joins on both DisplayName and GUID.

Also, I wasn't suggesting using a temp table for physician names, but a permanent one. Or adding the physician type column to an existing table like CV3User.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2011-04-12 : 11:55:04
quote:
Originally posted by sharona

Let me be blunt; what do you think you did right! I do not see anything. i didnt design the database and name the fields. the id codes are actual field names.

thank you those who are actually trying to help and not critize. i could create a temp table, but not so sure how to join that back the the query. ill give it a shot. thanks again for the help.



You're taking it all wrong My Sharona!

This is a forum, it is within our rights to suggest better ways to do things. I looked at your original issue and because of the lack of indentation in your posting I found it too dificult to answer. So that is another suggestion, when you post, try to use indentation. Again, not intended to offend you, this is a good suggestion. You need indent your code then to use the '#'.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2011-04-12 : 11:58:24
quote:
Originally posted by denis_the_thief

quote:
Originally posted by sharona

Let me be blunt; what do you think you did right! I do not see anything. i didnt design the database and name the fields. the id codes are actual field names.

thank you those who are actually trying to help and not critize. i could create a temp table, but not so sure how to join that back the the query. ill give it a shot. thanks again for the help.



You're taking it all wrong My Sharona!

This is a forum, it is within our rights to suggest better ways to do things. I looked at your original issue and because of the lack of indentation in your posting I found it too dificult to answer. So that is another suggestion, when you post, try to use indentation. Again, not intended to offend you, this is a good suggestion. You need indent your code then to use the '#'.



Oh, now I see. The post from jcelko, that was kind of nasty.
Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-12 : 12:21:31
thanks
Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-12 : 12:23:49
we can create permanant tables out of our hands on that. have to work with temp tables but those users are physicians were im going to create the one table with both thanks again. very helpful :)
Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-12 : 13:07:31
thank you that worked like a charm!!!
Go to Top of Page
   

- Advertisement -