Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top Value Per Unique ID? 3

Status
Not open for further replies.

danneedham

Programmer
Dec 19, 2007
30
GB
Hi All

I have been pondering over this one for a while... I'm quite a basic user trying to improve the systems at work.

I am trying to update the store database we currently have.

It consists of two tables. Store, which holds store id, name etc. And store status, which holds status date and status name.

I was wondering whether it was possible to have a query where the top value (most recent) is shown for each unique id (store)

Any help would be greatly appreciated.

Thanks


Dan
 
I suspect this is for academic purposes. As such, I will guide you, but not give you a solution.

In a case like this... forget about TOP. Instead, focus your attention on the Max aggregate and the group by clause.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hi There

I have just tried the MAX function by using the following statement:

SELECT StoreID, MAX(StatusDate) AS StatusDate
FROM Status
GROUP BY StoreID

-- How do i get the other information from the table into the query?? I would like to see the status label?

Many Thanks


Dan
 
A little light reading:

faq183-4785

Hope it helps,

Alex

[small]----signature below----[/small]
I'm pushing an elephant up the stairs

My Crummy Web Page
 
Select s.col1, s.col2, .... rest of column list...
From
Status s
Inner Join
(
SELECT StoreID, MAX(StatusDate) AS StatusDate
FROM Status
GROUP BY StoreID
) B ON
s.StoreID = b.StoreID
 
What jbenson001 is showing you is a [google]derived table[/google] method for building queries. This is a very powerful method to use, so it's important that you understand it completely. If you have any questions regarding derived tables, just let me know.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks all.

Think I'm going to have some reading up to do to completely understand this.

Thanks for all your help!


Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top