Solved

Help with using a case statement within a union

Posted on 2011-09-08
2
238 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
[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 Comments
 
LVL 5

Accepted Solution

by:
zvytas earned 500 total points
ID: 36503386
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
ID: 36504779
Perfect!  Thanks so much!!!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

734 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