| Author |
Topic |
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2011-12-29 : 14:38:53
|
Hi folks!I have two tables. Items and PriceItemsABCDEPricesItem Price Date RecidA 2 1/1/2011 ... A 3 5/1/2011 ...A 1 12/25/2011 ...B 6 12/5/2011 ...B 7 12/6/2011 ...I want to left join items to price where I get a 1:1 relationship.So results would be:Item Price DateA 1 12/25/2011B 7 12/6/2011Basically want to get the active price which is determined by max(Date). I've done this before but for some reason I'm getting lost. I know you have to left join and select the max date on an item to item level. something like:SELECT ITEMS.ITEM, PRICES.PRICE, PRICES.DATE FROM ITEMS LEFT JOIN (SELECT MAX(DATE) AS THEDATE, ITEM FROM PRICES GROUP BY DATE, ITEM) ON ITEMS.ITEM = PRICES.ITEM This doesn't give me the desired results. Any suggestions would be greatly appreciated! Thanks in advance. |
|
|
Dmh188
Starting Member
37 Posts |
Posted - 2011-12-29 : 15:01:34
|
| what about SELECT ITEMS.ITEMS, PRICE.PRICE, MAX(PRICE.DATE) FROM ITEMSINNER JOIN PRICEON PRICE.ITEM=ITEMS.ITEMGROUP BY ITEMS.ITEM, PRICE.PRICEsorry i have nothing close to this to test on. just going off the top of my head |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2011-12-29 : 15:09:59
|
quote: Originally posted by Dmh188 what about SELECT ITEMS.ITEMS, PRICE.PRICE, MAX(PRICE.DATE) FROM ITEMSINNER JOIN PRICEON PRICE.ITEM=ITEMS.ITEMGROUP BY ITEMS.ITEM, PRICE.PRICEsorry i have nothing close to this to test on. just going off the top of my head
I need the max price.date but I need the Price.Price record to go along with it. THis will not work unfortunately. Thank you for the reply. |
 |
|
|
Dmh188
Starting Member
37 Posts |
Posted - 2011-12-29 : 15:38:11
|
| My last thought here. On your Left join inner select. Try to group by item, date instead of date, item. If i remember correctly that has gotten me a few times |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-12-29 : 15:51:42
|
Unless you have specific requirement to use LEFT JOIN, either of the following should work.SELECT a.item, b.date, b.priceFROM Items a OUTER APPLY ( SELECT TOP 1 p.date,p.price FROM Prices p WHERE p.item = a.item ORDER BY p.date DESC ) b; ORSELECT s.item, s.date, s.priceFROM( SELECT a.Item, b.Price, b.date, ROW_NUMBER() OVER (PARTITION BY a.Item ORDER BY b.date DESC) AS RN FROM Items a LEFT JOIN Prices p ON a.item = p.item) sWHERE s.RN = 1; |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2012-01-03 : 08:47:19
|
quote: Originally posted by sunitabeck Unless you have specific requirement to use LEFT JOIN, either of the following should work.SELECT a.item, b.date, b.priceFROM Items a OUTER APPLY ( SELECT TOP 1 p.date,p.price FROM Prices p WHERE p.item = a.item ORDER BY p.date DESC ) b; ORSELECT s.item, s.date, s.priceFROM( SELECT a.Item, b.Price, b.date, ROW_NUMBER() OVER (PARTITION BY a.Item ORDER BY b.date DESC) AS RN FROM Items a LEFT JOIN Prices p ON a.item = p.item) sWHERE s.RN = 1;
Thank you sunitabeck! The first option worked like a charm. Really appreciate the response (and sorry for my late response)Happy new year! |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-03 : 09:11:26
|
| You are very welcome;And, Happy New Year to you as well. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-01-03 : 14:18:34
|
I mean really?Obsucating the actual code?CREATE TABLE #Price ([Item] char(1), [Price] int, [Date] date)GOINSERT INTO #Price([Item], [Price], [Date])SELECT 'A', 2, '1/1/2011' UNION ALLSELECT 'A', 3, '5/1/2011' UNION ALLSELECT 'A', 1, '12/25/2011' UNION ALLSELECT 'B', 6, '12/5/2011' UNION ALLSELECT 'B', 7, '12/6/2011'SELECT * FROM #Price o WHERE EXISTS (SELECT * FROM #Price i WHERE o.Item = i.Item GROUP BY Item HAVING o.[Date] = MAX(i.[Date]))GO Por Que?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
|