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

x
?
Solved

Pivot data

Posted on 2010-09-23
9
Medium Priority
?
257 Views
Last Modified: 2012-05-10
I have the following SQL query which returns the results with each netting id on a seperate row.

SELECT     cparty.name, cpidents.name AS nettingid, paymethd.name AS type
FROM         cparty INNER JOIN
                      cpidents ON cparty.thekey = cpidents.cparty INNER JOIN
                      paymethd ON cpidents.paymethd = paymethd.thekey
WHERE     (paymethd.name = N'CNSID-LOANS') or (paymethd.name = N'CNSID-FX') or (paymethd.name = N'CNSID-ROYALTY')


name                                      nettingid                             type
3206772 Nova Scotia Co.      1428                                   CNSID-FX
3206772 Nova Scotia Co.      1428                                   CNSID-LOAN    
3206772 Nova Scotia Co.      1428                                   CNSID-ROYALTY    
                                   
I would like it displayed like this

name                                               CNSID-FX      CNSID-LOAN        CNSID-ROYALTY    
3206772 Nova Scotia Co.                1428             1428                    1428

       


             
0
Comment
Question by:Dept808
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:carsRST
ID: 33746029
Here's an example using SQL Server's "pivot" - actually pretty close to your data.

http://www.mssqltips.com/tip.asp?tip=1019
0
 

Expert Comment

by:irenetheodora
ID: 33751440
I think this one should work fine.

SELECT     cparty.name,
      loans. nettingid as N'CNSID-LOANS',
      fx.nettingid as N'CNSID-FX',
      royalty.nettingid as N'CNSID-ROYALTY'
FROM        cparty
left join
  (select  cpidents.name as neetingid,
                cparty
      from  cpidents
      INNER JOIN paymethd
     ON cpidents.paymethd = paymethd.thekey
     where paymethd.name = N'CNSID-LOANS') loans
ON loans.cparty = cparty.thekey
left join
  (select     cpidents.name as neetingid,
                   cparty
      from     cpidents
      INNER JOIN paymethd
      ON cpidents.paymethd = paymethd.thekey
      where paymethd.name = N'CNSID-FX') fx
ON fx.cparty = cparty.thekey
left join
  (select     cpidents.name as neetingid,
                   cparty    
       from    cpidents
       INNER JOIN paymethd
      ON cpidents.paymethd = paymethd.thekey
       where paymethd.name = N'CNSID-ROYALTY') royalty
ON royalty.cparty = cparty.thekey
where
      loans. nettingid is not null and
      fx.nettingid is not null and
      royalty.nettingid is not null
   

0
 

Author Comment

by:Dept808
ID: 33753407
    loans.nettingid as N'CNSID-LOANS',
     fx.nettingid as N'CNSID-FX',
     royalty.nettingid as N'CNSID-ROYALTY'

Are not valid columns?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 3

Expert Comment

by:xiong8086
ID: 33759433
maybe try this following:

SELECT name, 'CNSID-LOANS' AS CNSID-LOANS, 'CNSID-FX' AS CNSID-FX, 'CNSID-ROYALTY' AS CNSID-ROYALTY
FROM
(SELECT     cparty.name, cpidents.name AS nettingid, paymethd.name AS type
FROM         cparty INNER JOIN
                      cpidents ON cparty.thekey = cpidents.cparty INNER JOIN
                      paymethd ON cpidents.paymethd = paymethd.thekey
WHERE     (paymethd.name = N'CNSID-LOANS') or (paymethd.name = N'CNSID-FX') or (paymethd.name = N'CNSID-ROYALTY')
) p
PIVOT
(
COUNT (nettingid)
FOR type IN
( 'CNSID-LOANS', 'CNSID-FX', 'CNSID-ROYALTY' )
) AS pvt
0
 

Expert Comment

by:irenetheodora
ID: 33767110
Dept808: yes they're valid actually. or you think I should put out another way?
0
 

Author Comment

by:Dept808
ID: 33769980
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'p'.
0
 
LVL 3

Expert Comment

by:xiong8086
ID: 33776038
put all the string values in [ ] like ['CNSID-FX'], then try again?
0
 

Author Comment

by:Dept808
ID: 33778679
I was able to rework the syntax and get results with the following query.  However I dont want a count of the values.. there will always be 1 or 0  I want to display the actual value, similar to my example.  The issue I now have is the value in the nettingid field is a nchar..

SELECT name, [CNSID-LOANS] AS "CNSID-LOANS", [CNSID-FX] AS "CNSID-FX", [CNSID-ROYALTY] AS "CNSID-ROYALTY"
FROM
(SELECT     cparty.name, cpidents.name AS nettingid, paymethd.name AS type
FROM         cparty INNER JOIN
                      cpidents ON cparty.thekey = cpidents.cparty INNER JOIN
                      paymethd ON cpidents.paymethd = paymethd.thekey
WHERE     (paymethd.name = N'CNSID-LOANS') or (paymethd.name = N'CNSID-FX') or (paymethd.name = N'CNSID-ROYALTY')
) p
PIVOT
(
count(nettingid)
FOR type IN
("CNSID-LOANS", "CNSID-FX", "CNSID-ROYALTY" )
) AS pvt
0
 
LVL 3

Accepted Solution

by:
xiong8086 earned 2000 total points
ID: 33781232
can try this?

SELECT name, ['CNSID-LOANS'] ,['CNSID-FX'] ,['CNSID-ROYALTY']
FROM
(SELECT     cparty.name, cpidents.name AS nettingid, paymethd.name AS type
FROM         cparty INNER JOIN
                      cpidents ON cparty.thekey = cpidents.cparty INNER JOIN
                      paymethd ON cpidents.paymethd = paymethd.thekey
WHERE     (paymethd.name = N'CNSID-LOANS') or (paymethd.name = N'CNSID-FX') or (paymethd.name = N'CNSID-ROYALTY')
) p
PIVOT
(
count(nettingid)
FOR type IN
('CNSID-LOANS', 'CNSID-FX', 'CNSID-ROYALTY' )
) AS pvt

can show the previous results?
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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