Solved

Simple Pivot query

Posted on 2015-01-30
6
83 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

696 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