Solved

Simple Pivot query

Posted on 2015-01-30
6
77 Views
Last Modified: 2015-02-11
I've never done a pivot query.  I need a simple pivot query.

I have a table that has records of "names", with a FK that column.  For example

FK      NAME
1         "Joe"
1         "Frank"
1         "Harold"
2         "Sue"
2         "Rachel"
etc, etc.

I need two (or however many) records based on FK, like

 "Joe", "Frank", "Harold"
"Sue", "Rachel"
0
Comment
Question by:HLRosenberger
6 Comments
 
LVL 33

Assisted Solution

by:ste5an
ste5an earned 167 total points
ID: 40580177
It's not a pivot, its row value concatenation. Normally you use the FOR XML PATH clause. E.g.

USE master;

WITH    Data
          AS ( SELECT   QUOTENAME(T.name) AS TableName ,
                        UPPER(LEFT(T.name, 1)) AS FirstLetter
               FROM     sys.tables T
             )
    SELECT  O.FirstLetter ,
            STUFF(( SELECT  ', ' + I.TableName
                    FROM    Data I
                    WHERE   I.FirstLetter = O.FirstLetter
                  FOR
                    XML PATH('')
                  ), 1, 2, '') AS TableNames
    FROM    Data O
    GROUP BY O.FirstLetter;

Open in new window

0
 
LVL 12

Assisted Solution

by:Habib Pourfard
Habib Pourfard earned 333 total points
ID: 40580390
You could write:
SELECT T1.FK,
       STUFF((SELECT (', ' + NAME) FROM dbo.Names T2
       WHERE T1.FK = T2.FK
       ORDER BY NAME
       FOR XML PATH ('')), 1, 2, '') [Names]
FROM dbo.Names T1
GROUP BY FK

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40580400
Give the attached code a whirl.
For some reason my EE UI is not allowing me to post this in a code block.
ee-text-wont-submit.txt
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:HLRosenberger
ID: 40580406
ok, I'm having trouble.  The data I need is in a joined table, admin_ethnicity.  Here's my query that returns all the rows based on where [FK_resource_home] = 225:  

SELECT [FK_resource_home], admin_ethnicity.name
  FROM .[dbo].[resource_home_ethnicity_preference]
  inner join admin_ethnicity ON admin_ethnicity.ID = [resource_home_ethnicity_preference].[FK_ethnicity]
 where [FK_resource_home] = 225


I get a result like this:
225   Caucasian
225  Hispanic or Latino

I want one row like this:

Caucasian; Hispanic or Latino;
0
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 333 total points
ID: 40580426
WITH Names AS (
SELECT [FK_resource_home], admin_ethnicity.name
  FROM .[dbo].[resource_home_ethnicity_preference]
  inner join admin_ethnicity ON admin_ethnicity.ID = [resource_home_ethnicity_preference].[FK_ethnicity]
 where [FK_resource_home] = 225
 ) 

 SELECT T1.[FK_resource_home],
       STUFF((SELECT (', ' + name) FROM Names T2
       WHERE T1.[FK_resource_home] = T2.[FK_resource_home]
       ORDER BY name
       FOR XML PATH ('')), 1, 2, '') [Names]
FROM Names T1
GROUP BY [FK_resource_home]

Open in new window

0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 40604053
thanks to all.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

895 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

11 Experts available now in Live!

Get 1:1 Help Now