[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Simple Pivot query

Posted on 2015-01-30
6
Medium Priority
?
90 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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

650 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