Я создал такую таблицу:
create table myTable (numero number
, name varchar2(50)
, price number
, coverage number
, activity_date date);
И вставил некоторые данные:
insert into myTable values (1, 'name1', 20, 50, to_date('02.10.2018','dd.mm.yyyy'));
insert into myTable values (2, 'name2', 10, 50, to_date('05.10.2018','dd.mm.yyyy'));
insert into myTable values (3, 'name3', 40, 50, to_date('02.10.2019','dd.mm.yyyy'));
insert into myTable values (4, 'name3', 60, 50, to_date('10.10.2019','dd.mm.yyyy'));
Это подзапрос:
SELECT numero
, name
, SUM(price) Amount
, coverage
, count(*)
, max(extract(year from activity_date)) year
FROM myTable
where (activity_date between TO_DATE('02.10.2018','dd.mm.yyyy')
and TO_DATE('20.10.2019','dd.mm.yyyy'))
group by numero
, name
, coverage having SUM(price) > 0;
И запрос с подзапросом:
SELECT coverage as coverages,
round(avg(case when year = 2018 then Amount end), 2) average2018,
count( case when year = 2018 then Amount end) count_2018,
round(avg(case when year = 2019 then Amount end), 2) average2019,
count( case when year = 2019 then Amount end) count_2019
FROM
(SELECT numero
, name
, SUM(price) Amount
, coverage
, count(*)
, max(extract(year from activity_date)) year
FROM myTable
where (activity_date between TO_DATE('02.10.2018','dd.mm.yyyy')
and TO_DATE('20.10.2019','dd.mm.yyyy'))
group by numero
, name
, coverage having SUM(price) > 0)
group by coverage;