Solved

How do I create a column header name for a case statement in Oracle SQL

Posted on 2010-08-19
5
496 Views
Last Modified: 2013-12-19
The following code pulls the data correctly put the name of the column header for the case statement is a concatation of all of the cases.  How do I assign 'PARTY' as the column header for the data pulled in the case statement?

SELECT DISTINCT
       ELECTIONS.ID,
       ELECTIONS.ELECTION_DATE,
       ELECTIONS.DESCRIPTION,
       JURISDICTION_DISTRICTS.NAME PRECINCTS,
       JURISDICTION_DISTRICTS.DISTRICT_CODE,
       JURISDICTIONS.NAME COUNTY,
       SPLITS.ID SPLIT,
       BALLOT_STYLES.CODE BALLOT_STYLE_NUMBER,
       ELECTION_RACES.BALLOT_ORDER,
      CANDIDATE_CONTESTS.BALLOT_POSITION CANDIDATE_ORDER_NUMBER,
      DISTRICT_TYPES.DESCRIPTION || ' ' || POSITIONS.POS_NAME "OFFICE TITLE",
      CANDIDATE_CONTESTS.BALLOT_NAME,
      POSITIONS.IS_WRITE_IN_ALLOWED,
      CASE  CANDIDATE_CONTESTS.POLPRT_ID
                        WHEN 2 THEN 'Democrat'
                     WHEN 3 THEN 'Republican'
                       WHEN 4 THEN 'Libertarian'
                   WHEN 5 THEN 'Reform'
                   WHEN 6 THEN 'Natural Law'
                   WHEN 8 THEN 'Constitution'
                   WHEN 9 THEN 'Green'
                   WHEN 10 THEN 'America First'
                   WHEN 11 THEN 'Independent'
                   WHEN 12 THEN 'Non Partisan'
                     ELSE ' '
                   END
         FROM ELECTIONS,
       SPLITS,
       SPLIT_DISTRICTS,
       DISTRICTS,
         DISTRICTS DISTRICTS_1,
       JURISDICTIONS,
         JURISDICTION_DISTRICTS,
       BALLOT_STYLE_SPLITS,
       BALLOT_STYLE_RACES,
       RACES,
       POSITIONS,
       CANDIDATE_CONTESTS,
       BALLOT_STYLES,
       DISTRICT_TYPES,
         ELECTION_RACES,
         POLITICAL_PARTIES
         WHERE     (ELECTIONS.ID = 575000643)
       AND (ELECTIONS.JS_CODE = '25')
       AND (SPLITS.IS_DELETED = 0)
         AND (SPLITS.ID) <16
       AND (SPLIT_DISTRICTS.IS_DELETED = 0)
       AND (DISTRICTS.IS_DELETED = 0)
       AND (DISTRICTS.DISTYP_ID = 3)
       AND (JURISDICTION_DISTRICTS.IS_DELETED = 0)
       AND (JURISDICTION_DISTRICTS.IS_FILING_OFF = 1)
       AND (ELECTIONS.IS_ARCHIVED = 0)
       AND (SPLIT_DISTRICTS.SPL_ID = SPLITS.ID)
       AND (JURISDICTION_DISTRICTS.DIS_ID = DISTRICTS.ID)
       AND (ELECTIONS.JS_CODE = JURISDICTIONS.CODE)
       AND (SPLIT_DISTRICTS.DIS_ID = DISTRICTS.ID)
       AND (JURISDICTIONS.CODE = JURISDICTION_DISTRICTS.JS_CODE)
       AND (BALLOT_STYLE_SPLITS.SPL_ID = SPLITS.ID)
       AND (BALLOT_STYLE_SPLITS.ELECBS_ID = BALLOT_STYLE_RACES.ELECBS_ID)
       AND (BALLOT_STYLE_RACES.RAC_ID = RACES.ID)
       AND (RACES.POS_ID = POSITIONS.ID)
       AND (CANDIDATE_CONTESTS.POS_ID = POSITIONS.ID)
       AND (CANDIDATE_CONTESTS.RAC_ID = RACES.ID)
       AND (BALLOT_STYLE_SPLITS.ELECBS_ID = BALLOT_STYLES.ID)
       AND (BALLOT_STYLE_RACES.ELECBS_ID = BALLOT_STYLES.ID)
       AND (BALLOT_STYLES.ELEC_ID = ELECTIONS.ID)
       AND (POSITIONS.DIS_ID = DISTRICTS_1.ID)
         AND (DISTRICTS_1.DISTYP_ID = DISTRICT_TYPES.ID)
         AND (ELECTION_RACES.ELEC_ID = ELECTIONS.ID)
       AND (ELECTION_RACES.JS_CODE = JURISDICTIONS.CODE)
       AND (ELECTION_RACES.RAC_ID = RACES.ID)
         ORDER BY JURISDICTION_DISTRICTS.NAME, ELECTION_RACES.BALLOT_ORDER,CANDIDATE_ORDER_NUMBER
        
Party.jpg
0
Comment
Question by:restockett
[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
5 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 125 total points
ID: 33480260
Just put it after the expression with or without the "AS" keyword, i.e.

      CASE  CANDIDATE_CONTESTS.POLPRT_ID
                        WHEN 2 THEN 'Democrat'
                     WHEN 3 THEN 'Republican'
                       WHEN 4 THEN 'Libertarian'
                   WHEN 5 THEN 'Reform'
                   WHEN 6 THEN 'Natural Law'
                   WHEN 8 THEN 'Constitution'
                   WHEN 9 THEN 'Green'
                   WHEN 10 THEN 'America First'
                   WHEN 11 THEN 'Independent'
                   WHEN 12 THEN 'Non Partisan'
                     ELSE ' '
                   END PartyName
         FROM ELECTIONS,
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 125 total points
ID: 33480261
CASE
...
END AS Party


And I believe the AS is optional
0
 
LVL 2

Expert Comment

by:thunderwonder
ID: 34041304
CASE  CANDIDATE_CONTESTS.POLPRT_ID
                        WHEN 2 THEN 'Democrat'
                     WHEN 3 THEN 'Republican'
                       WHEN 4 THEN 'Libertarian'
                   WHEN 5 THEN 'Reform'
                   WHEN 6 THEN 'Natural Law'
                   WHEN 8 THEN 'Constitution'
                   WHEN 9 THEN 'Green'
                   WHEN 10 THEN 'America First'
                   WHEN 11 THEN 'Independent'
                   WHEN 12 THEN 'Non Partisan'
                     ELSE ' '
                   END AS PARTY
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34328534
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to take different types of Oracle backups using RMAN.

733 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