Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Group By
Message
 
 
To
20/04/2009 18:38:39
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01395684
Message ID:
01395698
Views:
95
This message has been marked as the solution to the initial question of the thread.
There're many ways to get such result. Pick one that is most efficient for your data
* 1
SELECT * FROM mytable mt1 
  WHERE StartDate = (SELECT MAX(StartDate) FROM mytable mt2 where mt2.fk = mt1.fk)

* 2
SELECT * FROM mytable mt1 
  JOIN (SELECT fk, MAX(StartDate) AS MaxStartDate FROM mytable GROUP BY fk) dt1
    ON dt1.fk = mt1.fk AND dt1.MaxStartDate = mt1.StartDate 

* 3
SELECT * FROM mytable mt1 
  WHERE NOT EXISTS (SELECT 1 FROM mytable mt2 where mt2.fk = mt1.fk AND mt2.StartDate > mt1.StartDate)
>I'm having a group by brain-fart. Somebody please point me in the right direction...
>
>I have the following data:
>
>ID|FK|Status|StartDate
>1|1|Started|1/1/2008
>2|1|Finished|1/2/2008
>3|2|Started|3/2/2008
>4|3|Started|5/1/2008
>5|3|Finished|5/4/2008
>
>
>This is a transactional flow of items through statuses. I would like to see what status each item is currently set to, which should be the greatest StartDate. In my example, I want to see:
>
>FK|Status
>1|Finished
>2|Started
>3|Finished
>
>
>Thanks in advance!
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform