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
 Left join on max date to get one record

Author  Topic 

SQLSoaker
Posting Yak Master

169 Posts

Posted - 2011-12-29 : 14:38:53
Hi folks!

I have two tables. Items and Price

Items
A
B
C
D
E

Prices
Item Price Date Recid
A 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 Date
A 1 12/25/2011
B 7 12/6/2011

Basically 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 ITEMS
INNER JOIN PRICE
ON PRICE.ITEM=ITEMS.ITEM
GROUP BY ITEMS.ITEM, PRICE.PRICE


sorry i have nothing close to this to test on. just going off the top of my head
Go to Top of Page

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 ITEMS
INNER JOIN PRICE
ON PRICE.ITEM=ITEMS.ITEM
GROUP BY ITEMS.ITEM, PRICE.PRICE


sorry 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.
Go to Top of Page

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
Go to Top of Page

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.price
FROM
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;


OR

SELECT
s.item,
s.date,
s.price
FROM
(
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
) s
WHERE s.RN = 1;
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-29 : 15:57:32
WHERE EXISTS

I don't like all the nonsense now



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

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.price
FROM
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;


OR

SELECT
s.item,
s.date,
s.price
FROM
(
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
) s
WHERE 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!
Go to Top of Page

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.
Go to Top of Page

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)
GO

INSERT INTO #Price([Item], [Price], [Date])
SELECT 'A', 2, '1/1/2011' UNION ALL
SELECT 'A', 3, '5/1/2011' UNION ALL
SELECT 'A', 1, '12/25/2011' UNION ALL
SELECT 'B', 6, '12/5/2011' UNION ALL
SELECT '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?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -