Select top 1 (highest Id) child record for each parent

I have a Products table and a Movements table. I want to extract the latest movements table record for each Products record and I am unsure how to do it

I would like to return one line for each product:-
Products.Code, Products.Description. MOvements,Id, Movements,Date, Movements.Qty

showing only the most recent movement for each product.

Any ideas?

grwallaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ralmadaCommented:
One alternative (please note that I'm assuming that both tables are joined by a common column called "Code". adjust it accordingly.

select *
	from Products a
	inner join Movements b on a.Code = b.Code
	where b.Date = (select max([date]) from Movements where Code = a.Code)

Open in new window

0
käµfm³d 👽Commented:
How about:
SELECT p.Code, p.Description. m,Id, m.Date, m.Qty
FROM Products p
JOIN Movements m
WHERE Movements.Date = (SELECT MAX(n.Date) FROM Movements n WHERE n.Id = m.Id)

Open in new window

0
käµfm³d 👽Commented:
Slight correction:
SELECT p.Code, p.Description. m,Id, m.Date, m.Qty
FROM Products p
JOIN Movements m
WHERE m.Date = (SELECT MAX(n.Date) FROM Movements n WHERE n.Id = m.Id)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ralmadaCommented:
Another alternative if you have duplicate dates for the same product is
select * from
(
	select a.code, a.description, b.id, b.date, b.qty, row_number() over (partition by a.code order by b.date desc) rn
	from Products a
	inner join Movements b on a.Code = b.Code
) t1
where rn = 1

Open in new window

0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Try this:

Replace JOIN with your Primary and Foreign key column names and then execute it.

SELECT * FROM (
select Products.Code, Products.Description, MOvements.Id, Movements.Date, Movements.Qty, ROW_NUMBER() over ( partition by Products.Code
order by Movements.Date desc) rnum
FROM Products inner join Movements on Products.some_column = Movements.some_column ) temp
WHERE rnum = 1
0
ralmadaCommented:
rrjegan17, with respect, but I have suggested that in my comment http:#a30519322
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Yes ralmada..
I am having issues with my internet connection here and hence (very very) slow posting issue.
Kindly ignore..
0
käµfm³d 👽Commented:
NP. Glad to help  :)
0
ralmadaCommented:
With all due respect, I have posted that suggestion before kaufmed, see comment: http:#a30519110. Also his suggestion missed to link both tables. Can you please advise?
0
käµfm³d 👽Commented:
@ralmada

From my point of view, your solution assumed that the common field between the two tables was called "Code". However, I did not see the OP mention anywhere in post what the key field was for the two tables. My initial guess would have been Products.ID => Movements.Code, but I wasn't completely sure. I wrote my query to naturally pick the join columns.
0
ralmadaCommented:
>>I wrote my query to naturally pick the join columns.
I'm not sure what you are saying here. Also, I'm not sure about your syntax. I don't have a test enviroment here, but at first glance it doesn't look correct.
0
ralmadaCommented:
grwallace,
I would greatly appreciate you provide me with an explanation as to why my query didn't work for you.
0
ralmadaCommented:
Well, I have tested this and
SELECT p.Code, p.Description. m,Id, m.Date, m.Qty  FROM Products p  JOIN Movements m  WHERE m.Date = (SELECT MAX(n.Date) FROM Movements n WHERE n.Id = m.Id)
is not a valid syntax because it's missing the ON part to join both tables. The correct syntax is the as I've indicated in my comment http:#a30519110. So I will post an objection to reopen this question.
0
käµfm³d 👽Commented:
I concede that point. Apparently SQL Server does not support natural joins, which is what I was attempting. I'm not quite sure why the link I posted shows the ON as being an optional construct then, if natural joins are unsupported.
0
grwallaceAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.