Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 PIVOT Table

Author  Topic 

matta0990
Starting Member

44 Posts

Posted - 2012-03-22 : 05:20:57
Hi I'm trying to Pivot this SQL Query.

SELECT
Status_Name,
Sum(Trade_Freq) as Trades
FROM
Entrader.Entrader.Agreement_vw AG
LEFT OUTER JOIN Entrader.Core.Attribute_Values_vw AGST_ATVAL
ON AGST_ATVAL.Entity_Type = 'Agreement'
AND AGST_ATVAL.Attribute_Name = 'Agreement Status'
AND AG.Agreement_Id = AGST_ATVAL.Entity_Id
AND Is_Active = 1
left outer join(select A.Agreement_ID, Status_Name, Count(A.Trade_ID) as Trade_Freq
FROM
Entrader.Entrader.Trades_vw A
Inner Join Entrader.Entrader.Trade_Status_vw B ON A.TRADE_ID = B.TRADE_ID
WHERE
B.Updated = (Select max(B1.Updated) from Entrader.Entrader.Trade_Status_vw B1 where B1.Trade_Id = B.Trade_Id)
GROUP BY
A.Agreement_Id, Status_Name) Trd on AG.Agreement_ID = Trd.Agreement_ID
WHERE
AGST_ATVAL.Entry_Key IN ('New')
AND Commodity_Name in ('Biomass')
GROUP BY
Status_Name

Currently the output is:

Status_Name Trades

Dealer Approved 20
Ticket Approved 36
Trade Complete 5

I want to Pivot it so it looks like:

Dealer Approved Ticket Approved Trade Complete
20 36 5

The Trades chance daily so I don't know what they will be all the time.

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2012-03-22 : 05:40:05
This is the easiest to follow as it gives you the logical flow of PIVOT as well as the syntax:

http://msdn.microsoft.com/en-us/library/ms177410.aspx

If you are still struggling after reading this, come back and ask..
Go to Top of Page

matta0990
Starting Member

44 Posts

Posted - 2012-03-22 : 05:55:07
Yes, I tried this solution first but I could't get it to Pivot.

Matt
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2012-03-22 : 07:23:48
Something like this will do it, without sample data, I can't say it is 100% correct though. Also, if you have more Status_Names, you will need to add them.


select
'Trade Amounts' AS TradeAmount
, [Dealer Approved]
, [Ticket Approved]
, [Trade Complete]
from
(
SELECT
Status_Name,
Trade_Freq as Trades
FROM
Entrader.Entrader.Agreement_vw AG
LEFT OUTER JOIN Entrader.Core.Attribute_Values_vw AGST_ATVAL
ON AGST_ATVAL.Entity_Type = 'Agreement'
AND AGST_ATVAL.Attribute_Name = 'Agreement Status'
AND AG.Agreement_Id = AGST_ATVAL.Entity_Id
AND Is_Active = 1
left outer join(select A.Agreement_ID, Status_Name, Count(A.Trade_ID) as Trade_Freq
FROM
Entrader.Entrader.Trades_vw A
Inner Join Entrader.Entrader.Trade_Status_vw B ON A.TRADE_ID = B.TRADE_ID
WHERE
B.Updated = (Select max(B1.Updated) from Entrader.Entrader.Trade_Status_vw B1 where B1.Trade_Id = B.Trade_Id)
GROUP BY
A.Agreement_Id, Status_Name) Trd on AG.Agreement_ID = Trd.Agreement_ID
WHERE
AGST_ATVAL.Entry_Key IN ('New')
AND Commodity_Name in ('Biomass')
) getdata
pivot
(
SUM(Trades)
for
Status_Name IN ([Dealer Approved] , [Ticket Approved] , [Trade Complete])
) pvt
Go to Top of Page
   

- Advertisement -