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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help 1

Status
Not open for further replies.

jtrembla

ISP
Jul 6, 2006
104
US
VersionID ForecastID ModifiedDate
867 5 06-Dec-08
868 5 03-Dec-08
869 5 06-Dec-08
870 6 03-Dec-08
871 6 03-Dec-08
873 7 03-Dec-08
875 7 05-Dec-08

I need help writing a query that will return one version per forcastid where a version has the most recent modified date.

I tried using top but it only returns 1 record...
 
SELECT a.*
FROM YourTable a
INNER JOIN
(SELECT ForcecastID, MAX(ModifiedDate) AS MaxDate
FROM YourTable) b
ON a.ModifiedDate = b.MaxDate
 
Oops, forgot the GROUP BY and a join criteria (long day)

Code:
SELECT a.*
FROM YourTable a
INNER JOIN
  (SELECT ForcecastID, MAX(ModifiedDate) AS MaxDate
   FROM YourTable GROUP BY ForecastID) b
ON a.ForecastID = b.ForecastID AND a.ModifiedDate = b.MaxDate
 
I think you need to add
Code:
AND a.ForecastID = b.ForecasetID
 
I tried this and I still get multiple forecast ID returned.

SELECT a.*
FROM YourTable a
INNER JOIN
(SELECT ForcecastID, MAX(ModifiedDate) AS MaxDate
FROM YourTable GROUP BY ForecastID) b
ON a.ForecastID = b.ForecastID AND a.ModifiedDate = b.MaxDate
 
There is 2 of the same modified dates for multiple versions in a forecast, thus I think I need to pick the max version in my initial select?
 
Yes, also group and join on the max version. Or, if your version correlates to time, you can group on and join max version only.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top