If you want to select rows from Driver
along with the max()
and avg()
values for speed
, then use window functions:
SELECT d.*, CAST(d.date as DATE) as date,
MAX(d.speed) OVER (PARTITION BY CAST(d.date as DATE)) AS maxSpeed,
AVG(d.speed) OVER (PARTITION BY CAST(d.date as date)) AS avgSpeed
FROM Driver as d
WHERE d.date between '2014-01-01 09:00:00.491' and '2014-03-11 17:00:00.491'
ORDER BY date DESC;
EDIT:
If you want one row per driver, per day, then use a simple group by
:
SELECT d.userId, CAST(d.date as DATE) as date,
MAX(d.speed) as maxSpeed, AVG(d.speed) as avgSpeed
FROM Driver as d
WHERE d.date between '2014-01-01 09:00:00.491' and '2014-03-11 17:00:00.491'
GROUP BY d.userId, CAST(d.date as DATE)
ORDER BY date DESC;
If you want to add additional columns, you need to add them both to the select
and the group by
.