Solved

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

Posted on 2008-10-21
4
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

617 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