Для следующих запросов использовались упрощенная таблица STAFF_ORDERS и (пример) HR.EMPLOYEES таблица (Oracle 12c).Принцип: объедините все необходимые таблицы, сводку и фильтр.Может быть, этот пример поможет вам -
Тестовые таблицы и данные
create table staff_orders (
id number primary key
, employee_id number
, product_code varchar2( 5 )
, quantity number
);
insert into staff_orders values ( 1, 101, 'A', 10);
insert into staff_orders values ( 2, 101, 'B', 20);
insert into staff_orders values ( 3, 101, 'C', 30);
insert into staff_orders values ( 4, 102, 'A', 40);
insert into staff_orders values ( 5, 102, 'C', 50);
insert into staff_orders values ( 6, 103, 'A', 60);
insert into staff_orders values ( 7, 103, 'B', 70);
insert into staff_orders values ( 8, 103, 'C', 80);
insert into staff_orders values ( 9, 103, 'D', 90);
insert into staff_orders values (10, 104, 'A', 100);
insert into staff_orders values (11, 101, 'A', 10);
insert into staff_orders values (12, 101, 'B', 20);
insert into staff_orders values (13, 101, 'C', 30);
insert into staff_orders values (14, 102, 'A', 40);
insert into staff_orders values (15, 102, 'C', 50);
insert into staff_orders values (16, 103, 'A', 60);
insert into staff_orders values (17, 103, 'B', 70);
insert into staff_orders values (18, 103, 'C', 80);
insert into staff_orders values (19, 103, 'D', 90);
insert into staff_orders values (20, 104, 'A', 100);
commit;
Таблица STAFF_ORDERS
select * from staff_orders;
ID EMPLOYEE_ID PRODU QUANTITY
---------- ----------- ----- ----------
1 101 A 10
2 101 B 20
3 101 C 30
4 102 A 40
5 102 C 50
6 103 A 60
7 103 B 70
8 103 C 80
9 103 D 90
10 104 A 100
-- etc...
Таблица HR.EMPLOYEES
select employee_id, first_name, last_name
from hr.employees
where employee_id in ( 101, 102, 103, 104 ) ;
EMPLOYEE_ID FIRST_NAME LAST_NAME
102 Lex De Haan
104 Bruce Ernst
103 Alexander Hunold
101 Neena Kochhar
Присоединитесь к таблицам и скорректируйте столбцы по мере необходимости.
select
E.employee_id
, E.first_name || ' ' || E.last_name as employee_name
, O.product_code
, O.quantity
from hr.employees E
join staff_orders O on E.employee_id = O.employee_id ;
EMPLOYEE_ID EMPLOYEE_NAME PRODUCT_CODE QUANTITY
101 Neena Kochhar C 30
101 Neena Kochhar B 20
101 Neena Kochhar A 10
101 Neena Kochhar C 30
101 Neena Kochhar B 20
101 Neena Kochhar A 10
102 Lex De Haan C 50
-- ...
103 Alexander Hunold C 80
103 Alexander Hunold B 70
103 Alexander Hunold A 60
104 Bruce Ernst A 100
104 Bruce Ernst A 100
Сводка -> сумма количеств.
select *
from (
select
E.employee_id
, E.first_name || ' ' || E.last_name as employee_name
, O.product_code
, O.quantity
from hr.employees E
join staff_orders O on E.employee_id = O.employee_id
)
pivot (
sum( quantity ) as total for ( product_code ) in (
'A' AS product_a
, 'B' AS product_b
, 'C' AS product_c
)
)
;
EMPLOYEE_ID EMPLOYEE_NAME PRODUCT_A_TOTAL PRODUCT_B_TOTAL PRODUCT_C_TOTAL
104 Bruce Ernst 200 NULL NULL
102 Lex De Haan 80 NULL 100
103 Alexander Hunold 120 140 160
101 Neena Kochhar 20 40 60
Фильтр: всего продукта> 100 (добавьте предложение WHERE кпредыдущий запрос).
select *
from (
select
E.employee_id
, E.first_name || ' ' || E.last_name as employee_name
, O.product_code
, O.quantity
from hr.employees E
join staff_orders O on E.employee_id = O.employee_id
)
pivot (
sum( quantity ) as total for ( product_code ) in (
'A' AS product_a
, 'B' AS product_b
, 'C' AS product_c
)
)
where product_a_total > 100
or product_b_total > 100 -- use AND here if need be
or product_c_total > 100 -- use AND here if need be
;
EMPLOYEE_ID EMPLOYEE_NAME PRODUCT_A_TOTAL PRODUCT_B_TOTAL PRODUCT_C_TOTAL
104 Bruce Ernst 200 NULL NULL
103 Alexander Hunold 120 140 160