October 14, 2008 at 2:20 pm
Hi, I need to write a query which selects the most recent record entered from among multiple records which have the same column value, except for one single column, date entered.
this query returns all the values, but I only need the most recent one.
Select table1.column1, table2.column2, table3.column3,...
from table1,
join table2
on something = something else
join table3
on something = something else
where there is a condition
I am thinking I need to group these records based on the value of the date column, and then somehow select the most recent one.
Please help me.
October 14, 2008 at 2:26 pm
You could possibly use TOP 1 with an order by date desc.
Select Top 1
Column List
From
Table List
Where
Criteria
Order By
Date Column desc
Without more details this is the best I can do.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
October 14, 2008 at 2:45 pm
No, that wouldn't work. I am returning a table, and only some rows in this table might have multiple records, from which i need to select the most recent one.
I am thinking I need to use having clause after my group by, but not sure how. something like
having max(purchaseDate)
October 14, 2008 at 2:48 pm
Please post the table structure, some sample data and your desired output.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 14, 2008 at 2:53 pm
DATA:
product Id Product Name purchase DATE
11111 Book 2002
22222 Vase 2004
33333 PC 2006
33333 PC 2007
I'd like my query to return
product Id Product Name purchase DATE
11111 Book 2002
22222 Vase 2004
33333 PC 2007
October 14, 2008 at 2:56 pm
Okay, you should take a look at the links in my signature line as posting a more complete question helps you get better answers.
When you say you get multiple rows for some does that mean that you are getting the same data except for the date? Something like this:
FirstName LastName ChangeDate
--------- --------- ------------
Jack Corbett 01/27/1998
Jack Corbett 07/01/2008
Steve Jones 06/25/2007
Or are there other differences in the data as well?
If you post some schemas and some sample data it would be a lot easier to answer your question.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
October 14, 2008 at 3:01 pm
Okay with that data all you need is a Group By and a Max(PurchaseDate)
Select
product_id,
product_name,
Max(purchase_date) as last_purchase_date
From
table
Group By
product_id,
product_name
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
October 15, 2008 at 7:33 am
Thanks, sorry man bad. I should have mentioned that each record has a unique key, and this doesn't work.
DATA:
unique key product Id Product Name purchase DATE
1 11111 Book 2002
2 22222 Vase 2004
3 33333 PC 2006
4 33333 PC 2007
I'd like my query to return
Unique key product Id Product Name purchase DATE
1 11111 Book 2002
2 22222 Vase 2004
4 33333 PC 2007
October 15, 2008 at 7:59 am
Don't you mean for the last record you would want the record with unique Id 4?
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
October 15, 2008 at 8:01 am
That's why we ask for the table schema.
Assuming that the relationship between product_id and product_name is a 1-to-1, this should work
SELECT table.uniquekey, table.product_id, table.product_name, sub.last_purchase_date
FROM table INNER JOIN
(Select
product_id,
Max(purchase_date) as last_purchase_date
From
table
Group By
product_id) sub on table.product_id = sub.product_id AND table.purchase_date = sub.last_purchase_date
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 15, 2008 at 8:01 am
Yes, I do. I just want the last date or the first date. But in this case, let's go with the last date.
October 15, 2008 at 8:13 am
(note: you example is now showing data from 2 separate rows now. Just making sure you noticed that).
Considering product Id and Prodcut code seem to be the constants, groups by with aggregates would STILL work.
using Jack's sample:
Select
product_id,
product_name,
Max(purchase_date) as last_purchase_date
Min(uniqueKey) as firstkey
From
table
Group By
product_id,
product_name
Otherwise - try to post how the decision should be made (as to which rows should be picked. etc...
There are also some interesting things possible with a "windowed" ROW_NUMBER() function, which may have some bearing.
Anyway - take a look, and if that's not working, try to be specific as to how exactly the data should be pulled.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply