Avatar of Roberto Madro R.
Roberto Madro R.
Flag for United States of America asked on

Needs to extract the last incident from a group

Building a t-sql query, and I'm stuck at a problem, I have an Item_Number that has a Item_Manufacturing_Date, that ties into Item_Sales_Date, that ties into Item_Warehousing_Date that ties into Warehouse_Locations, I'm need to find out what is the last location an item was moved into in the warehouse. I've built most of the query, but I'm getting duplicates when I run it, I get all the locations an item was stored at, and I only want the very last location, my believe is my joint(s) is not defined properly somewhere along the way, I'm pressed for time and would really appreciate a swift reply,

SORRY, I can't share any piece of the data I'm working on so that you can help me, I appreciate your understanding and looking forward to hearing from you.

Many thanks
R.
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Roberto Madro R.

8/22/2022 - Mon
Ryan Chong

how's your SQL currently looks like?

Roberto Madro R.

ASKER
Since I cannot post any real code, up to the part I'm stuck on, I tried row_umber, I tried Lead and Lag, and still not getting exact code that will select the very last location the item is / was on, I've provided in the question some of the data elements involved and that should be of help, I hope.  Thx.
Ryan Chong

or can you provide some sample (masked or fake) data, so at least we can visualise the issue and provide a suggestion for you.

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
slightwv (䄆 Netminder)

>> SORRY, I can't share any piece of the data I'm working on so that you can help me


As Ryan suggested:  Make it up.  You can make up a test case similar to what you want with any data you can imagine.

Roberto Madro R.

ASKER
I'll put something together shortly.
Scott Pletcher

Something like this:


 SELECT mt.Item_Number, mt.Item_Manufacturing_Date, mt.Item_Sales_Date, 

    wt.Item_Warehousing_Date /*, ...*/
 FROM dbo.main_table mt
 OUTER APPLY (
     SELECT TOP (1) wt.*
     FROM dbo.warehouse_table wt
     WHERE wt.Item_Number = mt.Item_Number
     ORDER BY wt.Item_Warehousing_Date DESC
) AS wt

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
awking00

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Roberto Madro R.

ASKER
Thank you Experts, you always come through, sorry for the long delays in updating this case, the solution was right on the money.