Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-21
4
Medium Priority
?
565 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

885 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