Solved

Help with using a case statement within a union

Posted on 2011-09-08
2
232 Views
Last Modified: 2012-05-12
I am trying to modify a query so that I get different output depending on a specific value in one of the fields.  The query is something I found elsewhere and modified it to output a list of classes by course groups - like:

Group 100
 class 101
 class 102

Group 200
 class 201
 class 202

etc.

Now I need to have a different display only when the group level is 600 it will show something like:

Group 100
 class 101
 class 102

Group 200
 class 201
 class 202

Group 600
 no classes

I know I need a case but can't figure out how to incorporate that into my existing query - though I've been trying.  In the real query, I"m building some HTML but I've eliminated that here for the sake of clarity.  Anything that gives me alternate output for the 600 level will give me enough to work with.  The original query is attached.

Many thanks in advance.

SELECT Description
FROM
(
SELECT g.groupNum, 0 AS classId, g.groupName AS Description
FROM tbl_AdultEd_ClassGroups g where g.groupDisplay=1
UNION ALL
SELECT c.groupNum, c.classID, c.className AS Description 
FROM tbl_AdultEd_Classes c 
where c.classDisplay=1 
) sub
ORDER BY groupNum, classId

Open in new window

0
Comment
Question by:saabStory
2 Comments
 
LVL 5

Accepted Solution

by:
zvytas earned 500 total points
Comment Utility
Try the folliwing:

SELECT CASE WHEN classId <> 0 AND groupNum = 600 THEN 'no classes' ELSE Description  END AS Description
FROM
(
SELECT g.groupNum, 0 AS classId, g.groupName AS Description
FROM tbl_AdultEd_ClassGroups g where g.groupDisplay=1
UNION ALL
SELECT c.groupNum, c.classID, c.className AS Description
FROM tbl_AdultEd_Classes c
where c.classDisplay=1
) sub
ORDER BY groupNum, classId

I assumed level is stored in groupNum.
0
 

Author Closing Comment

by:saabStory
Comment Utility
Perfect!  Thanks so much!!!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

11 Experts available now in Live!

Get 1:1 Help Now