Home » SQL & PL/SQL » SQL & PL/SQL » min / max date of grouped status of a values ? (merged) (oracle enterprise rdbms 18.3, linux64)
min / max date of grouped status of a values ? (merged) [message #681662] |
Thu, 13 August 2020 05:18  |
flat
Messages: 8 Registered: September 2006
|
Junior Member |
|
|
Hi,
i would like to query the following table to get the proper result.
i cannot find any simple solution on this by myself.
maybe someone of you have a little trick on this.
here is my table:
date; service_name; status
13.08.2020 11:08; service_1; ok
13.08.2020 11:09; service_1; ok
13.08.2020 11:10; service_1; ok
13.08.2020 11:11; service_1; ok
13.08.2020 11:12; service_1; fail
13.08.2020 11:13; service_1; fail
13.08.2020 11:14; service_1; fail
13.08.2020 11:15; service_1; ok
13.08.2020 11:16; service_1; fail
13.08.2020 11:17; service_1; fail
13.08.2020 11:18; service_1; fail
The result should be grouped by (service_name;status) and look like this (availability-reporting):
service_name; status; min_date_status; max_date_status; count_status
service1; ok ; 13.08.2020 11:08; 13.08.2020 11:11; 4
service1; fail; 13.08.2020 11:12; 13.08.2020 11:14; 3
service1; ok; 13.08.2020 11:15; 13.08.2020 11:15; 1
service1; fail; 13.08.2020 11:16; 13.08.2020 11:18; 3
does anybody have some clue, how to solve this ?
many thanks in advance,
flat
|
|
|
|
|
Re: min / max date of grouped status of a values ? (merged) [message #681668 is a reply to message #681662] |
Thu, 13 August 2020 05:49   |
flat
Messages: 8 Registered: September 2006
|
Junior Member |
|
|
here are the inserts for a testcase:
CREATE TABLE availability_check (
check_time DATE,
service VARCHAR2(20),
status VARCHAR2(20)
);
insert into availability_check values (to_date('13.08.2020 11:08:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'ok');
insert into availability_check values (to_date('13.08.2020 11:09:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'ok');
insert into availability_check values (to_date('13.08.2020 11:10:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'ok');
insert into availability_check values (to_date('13.08.2020 11:11:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'ok');
insert into availability_check values (to_date('13.08.2020 11:12:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'fail');
insert into availability_check values (to_date('13.08.2020 11:13:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'fail');
insert into availability_check values (to_date('13.08.2020 11:14:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'fail');
insert into availability_check values (to_date('13.08.2020 11:15:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'ok');
insert into availability_check values (to_date('13.08.2020 11:16:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'fail');
insert into availability_check values (to_date('13.08.2020 11:17:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'fail');
insert into availability_check values (to_date('13.08.2020 11:18:00','DD.MM.YYYY HH24:MI:SS'), 'service_1', 'fail');
regards, flat
|
|
|
Re: min / max date of grouped status of a values ? (merged) [message #681669 is a reply to message #681668] |
Thu, 13 August 2020 06:26   |
 |
Michel Cadot
Messages: 68421 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
SQL> select service, check_time, status from availability_check order by 1, 2;
SERVICE CHECK_TIME STATUS
-------------------- ------------------- --------------------
service_1 13/08/2020 11:08:00 ok
service_1 13/08/2020 11:09:00 ok
service_1 13/08/2020 11:10:00 ok
service_1 13/08/2020 11:11:00 ok
service_1 13/08/2020 11:12:00 fail
service_1 13/08/2020 11:13:00 fail
service_1 13/08/2020 11:14:00 fail
service_1 13/08/2020 11:15:00 ok
service_1 13/08/2020 11:16:00 fail
service_1 13/08/2020 11:17:00 fail
service_1 13/08/2020 11:18:00 fail
11 rows selected.
SQL> with
2 data as (
3 select service, check_time, status,
4 case
5 when lag(status,1,'X') over (partition by service order by check_time) != status
6 then row_number() over (partition by service order by check_time)
7 end grp
8 from availability_check
9 ),
10 grouping as (
11 select service, check_time, status,
12 last_value(grp ignore nulls) over (partition by service order by check_time) grp
13 from data
14 )
15 select service, status, min(check_time) min_date_status, max(check_time) max_date_status, count(*) count_status
16 from grouping
17 group by service, status, grp
18 order by service, grp
19 /
SERVICE STATUS MIN_DATE_STATUS MAX_DATE_STATUS COUNT_STATUS
-------------------- -------------------- ------------------- ------------------- ------------
service_1 ok 13/08/2020 11:08:00 13/08/2020 11:11:00 4
service_1 fail 13/08/2020 11:12:00 13/08/2020 11:14:00 3
service_1 ok 13/08/2020 11:15:00 13/08/2020 11:15:00 1
service_1 fail 13/08/2020 11:16:00 13/08/2020 11:18:00 3
4 rows selected.
|
|
|
|
|
Goto Forum:
Current Time: Thu Mar 30 23:00:33 CDT 2023
|