Solved

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

Posted on 2013-01-18
2
665 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:
Scott Pletcher 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
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…

837 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