?
Solved

Simple Pivot query

Posted on 2015-01-30
6
Medium Priority
?
88 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
[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
6 Comments
 
LVL 35

Assisted Solution

by:ste5an
ste5an earned 668 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 1332 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 66

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 1332 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

777 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