Solved

Simple Pivot query

Posted on 2015-01-30
6
74 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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

706 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

19 Experts available now in Live!

Get 1:1 Help Now