Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with using a case statement within a union

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

618 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