?
Solved

Creating a list of values from a TSQL query

Posted on 2011-05-09
4
Medium Priority
?
277 Views
Last Modified: 2012-05-11
I have created a series of unions to create a list of values for 'Admission Tasks Outstanding'. I wondered if there is a more efficient way to achieve the same outcome?
SELECT     
dbc.encounterId
, dbc.BedNumber
, CASE WHEN BedNumber BETWEEN 1 AND 9 THEN 'POD1' 
WHEN BedNumber BETWEEN 10 AND 18 THEN 'POD2' 
WHEN BedNumber BETWEEN 19 AND 27 THEN 'POD3' 
WHEN BedNumber BETWEEN 27 AND 36 THEN 'POD4' END AS PODS
, dbc.ICUAdmDateTime
,CASE WHEN (dbc.encounterId NOT IN
	(SELECT encounterId FROM dbo.PtDemographic WITH (NOLOCK) WHERE (interventionId IN
			(SELECT interventionId 
				FROM dbo.D_Intervention WITH (NOLOCK)
				WHERE(shortLabel = ('Height (cm)') AND conceptcode = '50373000'))))) THEN 'Height' END AS 'Admission Tasks Outstanding'
FROM         
dbo.V_DailyBedCensus AS dbc 
INNER JOIN dbo.PtCensus AS c ON dbc.encounterId = c.encounterId
WHERE     
(c.inTime IS NOT NULL) AND 
(dbc.encounterId NOT IN (SELECT Encounterid FROM PMIMaster.dbo.RBWH_Excluded_Encounters))
UNION ALL
SELECT     
dbc.encounterId
, dbc.BedNumber
, CASE WHEN BedNumber BETWEEN 1 AND 9 THEN 'POD1' 
WHEN BedNumber BETWEEN 10 AND 18 THEN 'POD2' 
WHEN BedNumber BETWEEN 19 AND 27 THEN 'POD3' 
WHEN BedNumber BETWEEN 27 AND 36 THEN 'POD4' END AS PODS
, dbc.ICUAdmDateTime
,CASE WHEN (dbc.encounterId NOT IN
(SELECT encounterId FROM dbo.PtDemographic WITH (NOLOCK) WHERE (interventionId IN
			(SELECT interventionId
				FROM dbo.D_Intervention WITH (NOLOCK)
				WHERE(shortLabel = ('Weight (Kg) (Admission)') AND conceptcode = '27113001'))))) THEN 'Weight' END  AS 'Admission Tasks Outstanding'
FROM         
dbo.V_DailyBedCensus AS dbc 
INNER JOIN dbo.PtCensus AS c ON dbc.encounterId = c.encounterId
WHERE     
(c.inTime IS NOT NULL) AND 
(dbc.encounterId NOT IN (SELECT Encounterid FROM PMIMaster.dbo.RBWH_Excluded_Encounters))

UNION ALL
SELECT     
dbc.encounterId
, dbc.BedNumber
, CASE WHEN BedNumber BETWEEN 1 AND 9 THEN 'POD1' 
WHEN BedNumber BETWEEN 10 AND 18 THEN 'POD2' 
WHEN BedNumber BETWEEN 19 AND 27 THEN 'POD3' 
WHEN BedNumber BETWEEN 27 AND 36 THEN 'POD4' END AS PODS
, dbc.ICUAdmDateTime
, CASE WHEN (dbc.encounterId NOT IN
	(SELECT encounterId FROM dbo.PtIntervention WITH (NOLOCK) WHERE(interventionId IN
		(SELECT interventionId FROM dbo.D_Intervention WITH (NOLOCK)
		WHERE (shortLabel = ('Property List Completed') AND conceptcode = '370855008'))))) THEN 'PropertyList' END  AS 'Admission Tasks Outstanding'
FROM         
dbo.V_DailyBedCensus AS dbc 
INNER JOIN dbo.PtCensus AS c ON dbc.encounterId = c.encounterId
WHERE     
(c.inTime IS NOT NULL) AND 
(dbc.encounterId NOT IN (SELECT Encounterid FROM PMIMaster.dbo.RBWH_Excluded_Encounters))
ORDER BY DBC.BEDNUMBER

Open in new window

0
Comment
Question by:HiranB
4 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 35726284
This should help:



SELECT    
dbc.encounterId
, dbc.BedNumber
, CASE WHEN BedNumber BETWEEN 1 AND 9 THEN 'POD1'
WHEN BedNumber BETWEEN 10 AND 18 THEN 'POD2'
WHEN BedNumber BETWEEN 19 AND 27 THEN 'POD3'
WHEN BedNumber BETWEEN 27 AND 36 THEN 'POD4' END AS PODS
, dbc.ICUAdmDateTime
, CASE WHEN (dbc.encounterId NOT IN
      (SELECT encounterId FROM dbo.PtIntervention WITH (NOLOCK) WHERE(interventionId IN
            (SELECT interventionId FROM dbo.D_Intervention WITH (NOLOCK)
            WHERE (shortLabel = ('Property List Completed')
AND conceptcode in ('370855008','27113001',50373000'))))))
 THEN 'PropertyList' END  AS 'Admission Tasks Outstanding'
FROM        
dbo.V_DailyBedCensus AS dbc
INNER JOIN dbo.PtCensus AS c ON dbc.encounterId = c.encounterId
WHERE    
(c.inTime IS NOT NULL) AND
(dbc.encounterId NOT IN (SELECT Encounterid FROM PMIMaster.dbo.RBWH_Excluded_Encounters))


0
 

Author Comment

by:HiranB
ID: 35726337
Hi, Height and Weight (the first two select statements) come from ptdemographic and not ptintervention. How do I create the list when I am looking at multiple tables?
0
 
LVL 8

Accepted Solution

by:
Ghunaima earned 2000 total points
ID: 35726798
Try this

 sql1.sql
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 35727442
Its look good.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

862 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