MyTable
---------------------------------
time value
_________________________________
2023-10-13 00:08:00 15
2023-10-13 00:18:00 13
2023-10-13 00:28:00 14
2023-10-13 00:38:00 28
|
|
|
|
2023-10-13 23:38:00 23
2023-10-13 23:48:00 45
2023-10-13 23:58:00 25
2023-10-14 00:08:00 2
2023-10-14 00:18:00 64
|
|
2023-10-14 23:48:00 2
2023-10-14 23:58:00 6
|
|
2023-10-20 03:08:00 5
2023-10-20 03:18:00 93
2023-10-20 03:28:00 24
2023-10-20 03:38:00 58
The desired result should look like
Date startVal end val
2023-10-13 15 25
2023-10-14 2 6
Here's what I've tried:
select format(time, 'yyyy-MM-dd') as Date, min(value) as startVal, max(value) as endVal
from MyTable
where time between '2023-10-13' and '2023-10-20'
group by format(time, 'yyyy-MM-dd')
2条答案
按热度按时间vaj7vani1#
One way to do it would be to use a common table expression,
first_value
anddistinct
.First, create and populate sample table (Please save us this step in your future questions):
The query:
Results:
You can see a live demo on db<>fiddle
aij0ehis2#
See example
There 2
Sort
operations.