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
 Combining results into columns

Author  Topic 

ricc
Starting Member

16 Posts

Posted - 2013-10-18 : 07:08:35
Hi,
I have a query (SELECT * FROM Pricing) that produces the following results

Stockcode|ProductID|OurSellPrice|SupplierTypeID|CompetitorPrice
SC0001___|123______|22.45_______|1_____________|25.23
SC0001___|123______|21.45_______|2_____________|25.23

I want to convert this to the following :
Stockcode|ProductID|OurSellPriceType1|OurSellPriceType2|CompetitorPrice
SC001____|123______|22.45____________|21.45____________|25.23

We only have two types of suppliers but not every product is available from each type of supplier so we might get the following results:
Stockcode|ProductID|OurSellPrice|SupplierTypeID|CompetitorPrice
SC0002___|124______|22.45_______|1_____________|25.23

Stockcode|ProductID|OurSellPriceType1|OurSellPriceType2|CompetitorPrice
SC001____|123______|22.45____________|NULL_____________|25.23

Does anyone have any pointers?
Many thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-18 : 07:50:24
[code]
SELECT Stockcode,ProductID,
MAX(CASE WHEN SupplierTypeID = 1 THEN OurSellPrice END) AS OurSellPriceType1,
MAX(CASE WHEN SupplierTypeID = 2 THEN OurSellPrice END) AS OurSellPriceType2,
CompetitorPrice
FROM Pricing
GROUP BY Stockcode,ProductID,CompetitorPrice
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -