Я почти уверен, что это то, что вы хотите (я добавил немного данных для другой даты)
use tempdb
GO
DROP TABLE IF EXISTS #DATA
CREATE TABLE #DATA (
DG INTEGER
, Usage INTEGER
, UploadDateTime DATETIME2(3)
)
INSERT INTO #DATA (
DG, Usage, UploadDateTime
)
SELECT 1, 100, '2012-08-30 10:00:03.000' UNION ALL
SELECT 2, 500, '2012-08-30 10:00:03.000' UNION ALL
SELECT 3, 400, '2012-08-30 10:00:03.000' UNION ALL
SELECT 4, 550, '2012-08-30 10:00:03.000' UNION ALL
SELECT 5, 220, '2012-08-30 10:00:03.000' UNION ALL
SELECT 6, 110, '2012-08-30 10:00:03.000' UNION ALL
SELECT 1, 200, '2012-08-30 11:00:03.000' UNION ALL
SELECT 2, 600, '2012-08-30 11:00:03.000' UNION ALL
SELECT 3, 450, '2012-08-30 11:00:03.000' UNION ALL
SELECT 4, 200, '2012-08-30 11:00:03.000' UNION ALL
SELECT 5, 240, '2012-08-30 11:00:03.000' UNION ALL
SELECT 6, 50 , '2012-08-30 11:00:03.000' UNION ALL
SELECT 6, 51 , '2012-08-31 10:00:03.000' UNION ALL
SELECT 6, 52 , '2012-08-31 11:00:03.000'
SELECT * FROM #DATA
-- what is the max usage for each DG by date
SELECT DG, CAST(UploadDateTime AS DATE) AS UploadDate, MAX(Usage)
FROM #DATA
GROUP BY DG, CAST(UploadDateTime AS DATE)
-- using the max usage for each DG by date to join back to the orginal data
-- to get the datetime that relates to the max value
; WITH MAXES AS (
SELECT DG, CAST(UploadDateTime AS DATE) AS UploadDate, MAX(Usage) AS MaxUsage
FROM #DATA
GROUP BY DG, CAST(UploadDateTime AS DATE)
)
SELECT #DATA.*
FROM #DATA
INNER JOIN MAXES
ON #DATA.DG = MAXES.DG
AND CAST(#DATA.UploadDateTime AS DATE) = MAXES.UploadDate
AND #DATA.Usage = MaxUsage
ORDER BY UploadDate ASC, DG ASC