Solved

Help with using a case statement within a union

Posted on 2011-09-08
2
239 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
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…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

688 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