Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Need help with query....

Author  Topic 

focus_nz
Starting Member

2 Posts

Posted - 2004-08-30 : 18:09:01
I have a table called 'DiskDrive' which has the following data in it. The table contains the history of the free space on the specific disk drives.

-------------------------------------------------
| DiskID | DriveLetter | FreeSpace | Updated |
-------------------------------------------------
| 1 | C: | 50 | 2004-08-20 |
| 1 | C: | 100 | 2004-08-10 |
| 1 | A: | 0 | 2004-08-01 |
| 1 | C: | 500 | 2004-08-01 |
| 1 | D: | 100 | 2004-08-01 |
-------------------------------------------------


I need it to display all the distinct drive letters but only include the values from the lastest update.
So the recordset that I want to be returned is...

-------------------------------------------------
| DiskID | DriveLetter | FreeSpace | Updated |
-------------------------------------------------
| 1 | A: | 0 | 2004-08-01 |
| 1 | C: | 50 | 2004-08-20 |
| 1 | D: | 100 | 2004-08-01 |
-------------------------------------------------


Any idea of how I can write this into a single query I can create as a view?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-08-30 : 18:14:01
[code]

SELECT d2.DiskID, d2.DriveLetter, d1.FreeSpace, d2.Updated
FROM DiskDrive d1
INNER JOIN
(
SELECT DiskID, DriveLetter, MAX(Updated) AS Updated
FROM DiskDrive
GROUP BY DiskID, DriveLetter
) d2
ON d1.DiskID = d2.DiskID AND d1.DriveLetter = d2.DriveLetter AND d1.Updated = d2.Updated
ORDER BY d2.DiskID, d2.DriveLetter

[/code]

Tara
Go to Top of Page

focus_nz
Starting Member

2 Posts

Posted - 2004-08-30 : 18:23:31
Thanks, that was what I was after.
Go to Top of Page
   

- Advertisement -