I have a table:
abonent | region_id | dttm |
---|---|---|
7072110988 | 32722 | 2021-08-18 13:15 |
7072110988 | 32722 | 2021-08-18 14:00 |
7072110988 | 21534 | 2021-08-18 14:15 |
7072110988 | 32722 | 2021-08-19 09:00 |
7071107101 | 12533 | 2021-08-19 09:15 |
7071107101 | 32722 | 2021-08-19 09:27 |
Description of attributes:
- abonent – subscriber’s number;
- region_id – id of the region where the subscriber is located;
- dttm – the day and time of the call.
I need to determine the last location of the subscriber for each day.
Someone help please, I’m new to SQL(
2
Answers
Generate time series could solve the groupings and a MAX from the group should retrieve the last from the group.
The window function
row_number()
helps to enumerate abonents inside of a group (abonent, date(dttm)
).Also,
date()
function is used inside of apartition
-clause to group values by some date, not the full date-time value.See the demo.