August 19, 2015 at 8:11 pm
Hi,
ItemID ItemName Price
1 Test 1 100
1 Test 1 200
2 Test 2 130
2 Test 2 150
3 Test 3 300
3 Test 3 400
...
How to write a query to bring price data to column so the Item is not duplicate any more.
The result should be:
Item ID ITemName PRice1 PRice2
1 Test1 100 200
2 Test2 130 150
3 Test3 300 400
Any help would be greatly appreciated.
THanks,
Dee
August 19, 2015 at 8:42 pm
August 19, 2015 at 8:58 pm
Jayanth_Kurup (8/19/2015)
http://stackoverflow.com/questions/20057655/multiple-rows-in-one-column-sql-serverHave you seen this ?
That's concatenation rather than creating separate columns for common items.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 19, 2015 at 8:59 pm
Dee Dee-422077 (8/19/2015)
Hi,ItemID ItemName Price
1 Test 1 100
1 Test 1 200
2 Test 2 130
2 Test 2 150
3 Test 3 300
3 Test 3 400
...
How to write a query to bring price data to column so the Item is not duplicate any more.
The result should be:
Item ID ITemName PRice1 PRice2
1 Test1 100 200
2 Test2 130 150
3 Test3 300 400
Any help would be greatly appreciated.
THanks,
Dee
What's the maximum number of possible prices for each ItemID? Or does that need to be dynamic?
--Jeff Moden
Change is inevitable... Change for the better is not.
August 20, 2015 at 2:56 am
Quick static solution
π
USE RDSTEEST;
GO
SET NOCOUNT ON;
IF OBJECT_ID(N'dbo.TBL_SAMPLE_DATA') IS NOT NULL DROP TABLE dbo.TBL_SAMPLE_DATA;
CREATE TABLE dbo.TBL_SAMPLE_DATA
(
ItemID INT NOT NULL
,ItemName VARCHAR(20) NOT NULL
,Price DECIMAL(12,2) NOT NULL
);
INSERT INTO dbo.TBL_SAMPLE_DATA
(ItemID,ItemName,Price)
VALUES ( 1,'Test 1', 100)
,( 1,'Test 1', 200)
,( 2,'Test 2', 130)
,( 2,'Test 2', 150)
,( 3,'Test 3', 300)
,( 3,'Test 3', 400);
;WITH BASE_DATA AS
(
SELECT
SD.ItemID
,ROW_NUMBER() OVER
(
PARTITION BY SD.ItemID
ORDER BY (SELECT NULL)
) AS SD_RID
,SD.ItemName
,SD.Price
FROM dbo.TBL_SAMPLE_DATA SD
)
SELECT
BD.ItemID
,BD.ItemName
,MAX(CASE WHEN BD.SD_RID = 1 THEN BD.Price END) AS Price1
,MAX(CASE WHEN BD.SD_RID = 2 THEN BD.Price END) AS Price2
,MAX(CASE WHEN BD.SD_RID = 3 THEN BD.Price END) AS Price3
FROM BASE_DATA BD
GROUP BY BD.ItemID
,BD.ItemName
;
Results
ItemID ItemName Price1 Price2 Price3
------- --------- ------- ------- -------
1 Test 1 100.00 200.00 NULL
2 Test 2 130.00 150.00 NULL
3 Test 3 300.00 400.00 NULL
August 20, 2015 at 5:12 am
Hi Jeff,
Thanks for a quick reply.
The maximum price per itemid is 2.
Also the ItemID is dynamic
Thanks,
Dee
August 20, 2015 at 5:15 am
Thanks for the static solution.. But nn my table has thousand of rows so I really need the dynamic solution.
Best,
Dee
August 20, 2015 at 5:35 am
Dee Dee-422077 (8/20/2015)
Hi Jeff,Thanks for a quick reply.
The maximum price per itemid is 2.
Also the ItemID is dynamic
Thanks,
Dee
SELECT ItemID, ItemName,
Price1 = MIN(Price),
Price2 = MAX(Price)
FROM #TBL_SAMPLE_DATA
GROUP BY ItemID, ItemName
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 20, 2015 at 8:01 am
Jayanth_Kurup (8/20/2015)
@jeff Looks like a perfect example on using dynamixc SQL π
Nah. Not for this. It's a simple CROSSTAB or PIVOT problem, especially since it's known that there will only ever be two entries per item ID. CHRISM beat me to it on the solution (he usually does :-D) using a simple CROSSTAB.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 20, 2015 at 8:04 am
Eirikur's CROSSTAB solution is an effective solution for when the maximum number of items is 3 and can be easily converted for larger known maximums. It's also easily converted to solve, using dynamic SQL, for when the maximum number of items is unknown.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply