Solved

Pivot data

Posted on 2010-09-23
9
245 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
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
 
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

867 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

18 Experts available now in Live!

Get 1:1 Help Now