Solved

SQL Select same column, same field and return multiple aliases>

Posted on 2013-01-18
2
635 Views
Last Modified: 2013-01-18
Hello,
I am needing to create a table view that will return resutls for 7 different aliases from the same column.
Individual statements work as expected, how can I create a single statement to return all 7 values?
Or is there another approach I need to consider to accomplis this.
'char' fields ar varchar(160 and 'decimal' fields are decimal(21,6).
Thanks for any advice you can provide.

SELECT system_id, job_id, sub_job_id, sequence, free_field_char As [QAAuditSamples]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 28
SELECT system_id, job_id, sub_job_id, sequence, free_field_decimal As [QAQtyInspected]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 921
SELECT system_id, job_id, sub_job_id, sequence, free_field_decimal As [QASavesChecked]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 922
SELECT system_id, job_id, sub_job_id, sequence, free_field_decimal As [QADefectsFound]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 923
SELECT system_id, job_id, sub_job_id, sequence, free_field_decimal As [QAPiecesDefect]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 924
SELECT system_id, job_id, sub_job_id, sequence, free_field_decimal As [QADefectCode]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 925
SELECT system_id, job_id, sub_job_id, sequence, free_field_char As [QAVarianceApprovedBy]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 926
SELECT system_id, job_id, sub_job_id, sequence, free_field_char AS [QAComments]
            FROM jobfreefield
            WHERE system_id = 'ABC' and sequence = 927
0
Comment
Question by:okeefemd
2 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 38793799
Maybe something like this?:

SELECT system_id, job_id, sub_job_id,
    MAX(CASE WHEN sequence =  28 THEN free_field_char END) AS [QAAuditSamples],
    MAX(CASE WHEN sequence = 921 THEN free_field_decimal END) AS [QAQtyInspected],
    MAX(CASE WHEN sequence = 922 THEN free_field_decimal END) AS [QASavesChecked],
    MAX(CASE WHEN sequence = 923 THEN free_field_decimal END) AS [QADefectsFound],
    MAX(CASE WHEN sequence = 924 THEN free_field_decimal END) AS [QAPiecesDefect],
    MAX(CASE WHEN sequence = 925 THEN free_field_decimal END) AS [QADefectCode],
    MAX(CASE WHEN sequence = 926 THEN free_field_char END) AS [QAVarianceApprovedBy],
    MAX(CASE WHEN sequence = 927 THEN free_field_char END) AS [QAComments]
FROM dbo.jobfreefield
WHERE system_id = 'ABC'
GROUP BY
    system_id, job_id, sub_job_id
ORDER BY
    system_id, job_id, sub_job_id
0
 

Author Closing Comment

by:okeefemd
ID: 38793884
Scott, this returned the results I am looking for.  Excelent!  Thank you very much.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now