March 25, 2011 at 4:05 am
Hi
I have a table looking like:
item,pricecode,price
1018,1,100
1020,2,120
1031,1,90
1045,5,45
1018,2,120
1430,7,35
1020,3,95
1430,5,30
Now I want to create a table tha only holds items once and based in price priority.
Price priority: 1,3,2,5,7
My new table should look like;
item,pricecode,price
1018,1,100
1020,3,95
1031,1,90
1045,5,45
1430,5,30
How can I solve this best possible?
Dan
March 25, 2011 at 4:12 am
DECLARE @T TABLE(item INT,pricecode INT,price INT)
INSERT INTO @T(item,pricecode,price)
SELECT 1018,1,100 UNION ALL
SELECT 1020,2,120 UNION ALL
SELECT 1031,1,90 UNION ALL
SELECT 1045,5,45 UNION ALL
SELECT 1018,2,120 UNION ALL
SELECT 1430,7,35 UNION ALL
SELECT 1020,3,95 UNION ALL
SELECT 1430,5,30;
WITH CTE AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY item ORDER BY price) AS rn
FROM @T)
DELETE FROM CTE
WHERE rn>1;
SELECT * FROM @T ORDER BY item
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537March 25, 2011 at 4:56 am
Thanks Mark.
That was just brilliant.
Short end efective.
Dan
March 25, 2011 at 5:41 am
Dan-Ketil Jakobsen (3/25/2011)
Thanks Mark.That was just brilliant.
Short end efective.
Dan
Thanks for the feedback!
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply