?
Solved

Help with using a case statement within a union

Posted on 2011-09-08
2
Medium Priority
?
240 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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 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