Solved

Pivot data

Posted on 2010-09-23
9
244 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:carsRST
Comment Utility
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
Comment Utility
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
Comment Utility
    loans.nettingid as N'CNSID-LOANS',
     fx.nettingid as N'CNSID-FX',
     royalty.nettingid as N'CNSID-ROYALTY'

Are not valid columns?
0
 
LVL 3

Expert Comment

by:xiong8086
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

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

Author Comment

by:Dept808
Comment Utility
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
Comment Utility
put all the string values in [ ] like ['CNSID-FX'], then try again?
0
 

Author Comment

by:Dept808
Comment Utility
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 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL 2012 and SQL 2014 always on 9 23
Date conversion in sql server 2012 6 25
SQL Server 2012 Row Selection 2 29
Azure SQL DB? 3 12
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
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.

772 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

11 Experts available now in Live!

Get 1:1 Help Now