Solved

Microsoft, SQL, 2005, Union and Case Statement Help Needed

Posted on 2008-10-21
4
557 Views
Last Modified: 2012-06-27
I have 6 DoctorFacility Types (Doctor = 1, Facility = 2, Referring Doctor = 3, Company = 5, Resource = 6
and Other Provider = 7)

With my Code Snippet below, I am getting the following results back:

Text      ItemData
NULL      1
NULL      2
NULL      3
NULL      5
NULL      6
NULL      7
(All)      0
Doctor      1
Other Provider      7
Referring Doctor      3

What I want only:

(All)      0
Doctor      1
Other Provider      7
Referring Doctor      3

I dont want to see the others in my list ... just exactly what I have above this sentence.
SELECT

        '(All)' AS Text,

        0 AS ItemData

UNION 

SELECT

        CASE WHEN TYPE = 1 THEN 'Doctor' END AS TEXT,

        type AS Itemdata

FROM

        DoctorFacility

UNION 

SELECT 

        CASE WHEN TYPE = 3 THEN 'Referring Doctor' END AS TEXT,

        type AS Itemdata

FROM

        DoctorFacility

UNION 

SELECT 

        CASE WHEN TYPE = 7 THEN 'Other Provider' END AS TEXT,

		type AS Itemdata    

FROM

        DoctorFacility

Open in new window

0
Comment
Question by:Jeff S
  • 2
4 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 22773221
try this
SELECT 

CASE  

WHEN type=1 THEN 'Doctor'

.....

END AS TEXT

,type AS Itemdata

FROM

        DoctorFacility

GROUP BY type 

Open in new window

0
 
LVL 7

Author Comment

by:Jeff S
ID: 22773242
HuyBD

I modified your coding slightly to this:

SELECT
        '(All)' AS Text,
        0 AS ItemData
UNION
SELECT
CASE  
WHEN type=1 THEN 'Doctor'
WHEN TYPE = 3 THEN 'Referring Doctor'
WHEN TYPE = 7 THEN 'Other Provider'
END AS TEXT
,type AS Itemdata
FROM
        DoctorFacility
GROUP BY type

But now get this:
Text                     ItemData
NULL                        2
NULL                        5
NULL                        6
(All)                        0
Doctor                        1
Other Provider        7
Referring Doctor        3

I do not want ItemData's 2, 5 or 6.
0
 
LVL 5

Expert Comment

by:jfmador
ID: 22773248
There is null value because you only specify one value in your case statement

Take a look to your query, what happen for the doctor facility 2 to 7, they will return null for the text.
SELECT
        CASE WHEN TYPE = 1 THEN 'Doctor' END AS TEXT,
        type AS Itemdata
FROM
        DoctorFacility


Try this

SELECT
        '(All)' AS Text,
        0 AS ItemData
UNION ALL
SELECT
        CASE WHEN TYPE = 1 THEN 'Doctor'
                  WHEN TYPE = 2 THEN 'Facility'
                  WHEN TYPE = 3 THEN 'Referring Doctor'
                  WHEN TYPE = 4 THEN ''
                  WHEN TYPE = 5 THEN 'Company'
                  WHEN TYPE = 6 THEN 'Resource'
                  WHEN TYPE = 7 THEN 'Other Provider' END AS TEXT,
        type AS Itemdata
FROM
        DoctorFacility

You can add where type in (1,3,7) if you want only these choice
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 500 total points
ID: 22773252
try to add more condition to move unselected item out
SELECT

        '(All)' AS Text,

        0 AS ItemData

UNION

SELECT

CASE  

WHEN type=1 THEN 'Doctor'

WHEN TYPE = 3 THEN 'Referring Doctor'

WHEN TYPE = 7 THEN 'Other Provider'

END AS TEXT

,type AS Itemdata

FROM

        DoctorFacility

WHERE TYPE IN(1,3,7)

GROUP BY type 

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

21 Experts available now in Live!

Get 1:1 Help Now