Solved

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

Posted on 2008-10-21
4
559 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

830 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