Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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