Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-18
2
Medium Priority
?
695 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

971 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