How to Compare Rows in SQL?

I need help transposing Rows to Columns in Microsoft SQL Server 2008 R2?

  • I'm working with two tables: ITEM & PriceList. My values in each table looks as Follows ITEM Table: ID (PrimaryKey) | Code --------------------------------------... 1 | Item_One 2 | Item_Two PricelIst Table PriceID (Pk) | PricelistName | ItemId (Fk in Item Table) --------------------------------------... 1 | Retail | 1 2 | Wholesale | 1 3 | Retail | 2 4 | Wholesale | 2 If i join the tables on the stock ID - It brings it in underneeath eachother (Rows Format) I need it to be in Column view. i.e: i would like to see the following result? ID (Pk) | Code | RetailPrice | WholesalePrice -------------------------------------- ----------------------------------------... 1 | Item_One | $X.xx | $Y.yy | 2 | Item_Two | $X.xx | $Y.yy | I did not put in the prices in the Price table above but it is there (It's called Price) Please asssist or point me in the correct direction - I've tried "googling transpose in SQL" but I get so many different results - Some referring to Pivots & Others referring to Unions? I'm not sure which I should be investigating (My feel is that this should be simple group by or something?) I'll really appreciate it if you can maybe reply with an script example. What ever input will be appreciated. Thanks in Advance

  • Answer:

    As SQL Server 2008 is equipped with advanced Operators like PIVOT to ease your task.. lets use it. Based on the data from your problem statement, I have recreated the schemas and wrote the below script for you.. tell us if our effort has in any way helped you. CREATE TABLE #Item ( Id INT, Code VARCHAR ) CREATE TABLE #PriceList ( PriceId INT, PriceListName VARCHAR(50), ItemId INT ) CREATE TABLE #Stock ( StockId INT, PriceId VARCHAR, ItemId INT, Price INT ) INSERT #Item VALUES(1,'X'),(2,'Y'),(3,'Z') INSERT #PriceList VALUES (1,'RetailPrice',1), (2,'WholesalePrice',1), (3,'RetailPrice',2), (4,'WholesalePrice',2), (5,'RetailPrice',3) INSERT #Stock VALUES(1,1,1,20),(2,2,1,10), (3,3,2,120),(4,4,2,200),(5,5,3,230) SELECT * FROM #Item SELECT * FROM #PriceList SELECT * FROM #Stock /*********** PIVOT usage for transposing *************/ SELECT Id,Code,RetailPrice ,WholesalePrice FROM( SELECT It.Id,It.Code,PL.PriceListName,St.Price FROM #Item It JOIN #PriceList PL ON PL.ItemId=It.Id JOIN #Stock St ON St.ItemId=PL.ItemId AND St.PriceId = PL.PriceId ) SQ PIVOT ( SUM(Price) FOR PriceListName IN (RetailPrice ,WholesalePrice) ) AS PVT -- In 'thoughts'... Lonely Rogue. https://twitter.com/#!/LonelyRogue

Jackal0510 at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

As SQL Server 2008 is equipped with advanced Operators like PIVOT to ease your task.. lets use it. Based on the data from your problem statement, I have recreated the schemas and wrote the below script for you.. tell us if our effort has in any way helped you. CREATE TABLE #Item ( Id INT, Code VARCHAR ) CREATE TABLE #PriceList ( PriceId INT, PriceListName VARCHAR(50), ItemId INT ) CREATE TABLE #Stock ( StockId INT, PriceId VARCHAR, ItemId INT, Price INT ) INSERT #Item VALUES(1,'X'),(2,'Y'),(3,'Z') INSERT #PriceList VALUES (1,'RetailPrice',1), (2,'WholesalePrice',1), (3,'RetailPrice',2), (4,'WholesalePrice',2), (5,'RetailPrice',3) INSERT #Stock VALUES(1,1,1,20),(2,2,1,10), (3,3,2,120),(4,4,2,200),(5,5,3,230) SELECT * FROM #Item SELECT * FROM #PriceList SELECT * FROM #Stock /*********** PIVOT usage for transposing *************/ SELECT Id,Code,RetailPrice ,WholesalePrice FROM( SELECT It.Id,It.Code,PL.PriceListName,St.Price FROM #Item It JOIN #PriceList PL ON PL.ItemId=It.Id JOIN #Stock St ON St.ItemId=PL.ItemId AND St.PriceId = PL.PriceId ) SQ PIVOT ( SUM(Price) FOR PriceListName IN (RetailPrice ,WholesalePrice) ) AS PVT -- In 'thoughts'... Lonely Rogue. https://twitter.com/#!/LonelyRogue

Lonely Rogue

I'm assuming that the price value is also a column for pricelist. Also, if there is always both a retail and wholesale price, you can eliminate the COALESCE and make an inner join instead of outer. What you need is a selfjoin on pricelist like so: SELECT i.id AS "ID (PK)", Code, COALESCE(r.price, "N/A") AS "Retail Price", COALESCE(w.price, "N/A") FROM item i LEFT OUTER JOIN (SELECT * FROM pricelist WHERE PricelistName = "Retail") r ON i.PriceID = r.ID LEFT OUTER JOIN (SELECT * FROM pricelist WHERE PricelistName = "Wholesale") w ON i.PriceID = w.ID

TheMadProfessor

Include your price table twice. Filter to only show retail from the first and wholesale from the second. Select Code from item table, retail price from first price table, wholesale price from second price table

How would I Know

I'm assuming that the price value is also a column for pricelist. Also, if there is always both a retail and wholesale price, you can eliminate the COALESCE and make an inner join instead of outer. What you need is a selfjoin on pricelist like so: SELECT i.id AS "ID (PK)", Code, COALESCE(r.price, "N/A") AS "Retail Price", COALESCE(w.price, "N/A") FROM item i LEFT OUTER JOIN (SELECT * FROM pricelist WHERE PricelistName = "Retail") r ON i.PriceID = r.ID LEFT OUTER JOIN (SELECT * FROM pricelist WHERE PricelistName = "Wholesale") w ON i.PriceID = w.ID

TheMadProfessor

Include your price table twice. Filter to only show retail from the first and wholesale from the second. Select Code from item table, retail price from first price table, wholesale price from second price table

How would I Know

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.