DROP TABLE IF EXISTS clients;
CREATE TABLE clients
(
id number(10),
name varchar2(1000),
place_of_birth varchar2(2000),
date_of_birth date,
address varchar2(2000),
passport varchar2(1000),
CONSTRAINT clients_pk primary key (id)
);
DROP TABLE IF EXISTS products;
CREATE TABLE products
(
id number(10),
product_type_id number(10),
name varchar2(1000),
client_ref number(10),
open_date date,
close_date date,
CONSTRAINT products_pk primary key (id)
);
DROP TABLE IF EXISTS product_type;
CREATE TABLE product_type
(
id number(10),
name varchar2(100),
begin_date date,
end_date date,
tarif_ref number(10),
CONSTRAINT product_type_pk primary key (id)
);
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts
(
id number(10),
name varchar2(1000),
saldo number(10,2),
client_ref number(10),
open_date date,
close_date date,
product_ref number(10),
acc_num varchar2(25),
CONSTRAINT accounts_pk primary key (id)
);
DROP TABLE IF EXISTS records;
CREATE TABLE records
(
id number(10),
dt number(1),
sum number(10,2),
acc_ref number(10),
oper_date date,
CONSTRAINT records_pk primary key (id)
);
DROP TABLE IF EXISTS tarifs;
CREATE TABLE tarifs
(
id number(10),
name varchar2(1000),
cost number (10,2),
CONSTRAINT tarifs_pk primary key (id)
);
ALTER TABLE products
ADD CONSTRAINT prod_cl_fk
FOREIGN KEY (client_ref)
REFERENCES clients(id);
ALTER TABLE products
ADD CONSTRAINT prod_prodtype_fk
FOREIGN KEY (product_type_id)
REFERENCES product_type(id);
ALTER TABLE product_type
ADD CONSTRAINT product_type_fk
FOREIGN KEY (tarif_ref)
REFERENCES tarifs(id);
ALTER TABLE records
ADD CONSTRAINT rec_acc_fk
FOREIGN KEY (acc_ref)
REFERENCES accounts(id);
ALTER TABLE accounts
ADD CONSTRAINT acc_cl_fk
FOREIGN KEY (client_ref)
REFERENCES clients(id);
ALTER TABLE accounts
ADD CONSTRAINT acc_prod_fk
FOREIGN KEY (product_ref)
REFERENCES products(id);
insert into tarifs values (1,'Тариф за выдачу кредита', 10);
insert into tarifs values (2,'Тариф за открытие счета', 10);
insert into tarifs values (3,'Тариф за обслуживание карты', 10);
insert into product_type values (1, 'КРЕДИТ', to_date('01.01.2018','DD.MM.YYYY'), null, 1);
insert into product_type values (2, 'ДЕПОЗИТ', to_date('01.01.2018','DD.MM.YYYY'), null, 2);
insert into product_type values (3, 'КАРТА', to_date('01.01.2018','DD.MM.YYYY'), null, 3);
insert into clients values (1, 'Сидоров Иван Петрович', 'Россия, Московская облать, г. Пушкин',
to_date('01.01.2001','DD.MM.YYYY'), 'Россия, Московская облать, г. Пушкин, ул. Грибоедова, д. 5', '2222 555555, выдан
ОВД г. Пушкин, 10.01.2015');
insert into clients values (2, 'Иванов Петр Сидорович', 'Россия, Московская облать, г. Клин',
to_date('01.01.2001','DD.MM.YYYY'), 'Россия, Московская облать, г. Клин, ул. Мясникова, д. 3', '4444 666666, выдан ОВД г.
Клин, 10.01.2015');
insert into clients values (3, 'Петров Сиодр Иванович', 'Россия, Московская облать, г. Балашиха',
to_date('01.01.2001','DD.MM.YYYY'), 'Россия, Московская облать, г. Балашиха, ул. Пушкина, д. 7', '4444 666666, выдан ОВД
г. Клин, 10.01.2015');
insert into clients values (4, 'Сериков Виталий Валерьевич', 'Германия г. Берлин',
to_date('25.09.1981','DD.MM.YYYY'), 'Россия, Амурская область, г. Благовещенск, ул. Шевченко д.44 кв 43', '6546 654884, выдан УВД
Свободного, 03.03.2003');
insert into clients values (5, 'Петров Вениамин Иванович', 'г. Анадырь ЧАО',
to_date('20.12.1990','DD.MM.YYYY'), 'Россия, Амурская область, г. Благовещенск, ул. Пионераская д. 22 ', '4588 4584851, выдан УВД
Благовещенска, 15.05.2013');
insert into products values (1, 1, 'Кредитный договор с Сидоровым И.П.', 1, to_date('01.06.2015','DD.MM.YYYY'), null);
insert into products values (2, 2, 'Депозитный договор с Ивановым П.С.', 2, to_date('01.08.2017','DD.MM.YYYY'), null);
insert into products values (3, 3, 'Карточный договор с Петровым С.И.', 3, to_date('01.08.2017','DD.MM.YYYY'), null);
insert into products values (4, 2, 'Депозитный договор с Сериковым В.В.', 4, to_date('01.08.2015','DD.MM.YYYY'), null);
insert into products values (4, 3, 'Карточный договор с Сериковым В.В.', 4, to_date('01.08.2016','DD.MM.YYYY'), null);
insert into products values (4, 1, 'Кредитный договор с Сериковым В.В.', 4, to_date('01.08.2023','DD.MM.YYYY'), null);
insert into products values (4, 2, 'Депозитный договор с Петровым В.И.', 5, to_date('15.01.2024','DD.MM.YYYY'), null);
insert into products values (4, 3, 'Карточный договор с Петровым В.И.', 5, to_date('16.01.2024','DD.MM.YYYY'), null);
insert into accounts values (4, 'Депозитный счет для Серикова В.В.', 150000, 4, to_date('01.08.2015','DD.MM.YYYY'), null, 1,
'45502810401020000075');
insert into accounts values (5, 'Карточный счет для Серикова В.В.', 132000, 4, to_date('01.08.2016','DD.MM.YYYY'), null, 1,
'45502810401020000075');
insert into accounts values (6, 'Кредитный счет для Серикова В.В.', -650000, 4, to_date('01.08.2016','DD.MM.YYYY'), null, 1,
'45502810401020000075');
insert into accounts values (7, 'Депозитный счет для Петрова В.И.', 1300000, 5, to_date('05.05.2024','DD.MM.YYYY'), null, 1,
'45502810401020000075');
insert into accounts values (8, 'Карточный счет для Серикова В.В.', 5000, 5, to_date('05.05.2024','DD.MM.YYYY'), null, 1,
'45502810401020000075');
insert into accounts values (1, 'Кредитный счет для Сидоровым И.П.', -2000, 1, to_date('01.06.2015','DD.MM.YYYY'), null, 1,
'45502810401020000022');
insert into accounts values (2, 'Депозитный счет для Ивановым П.С.', 6000, 2, to_date('01.08.2017','DD.MM.YYYY'), null, 2,
'42301810400000000001');
insert into accounts values (3, 'Карточный счет для Петровым С.И.', 8000, 3, to_date('01.08.2017','DD.MM.YYYY'), null, 3,
'40817810700000000001');
insert into records values (1, 1, 5000, 1, to_date('01.06.2015','DD.MM.YYYY'));
insert into records values (2, 0, 1000, 1, to_date('01.07.2015','DD.MM.YYYY'));
insert into records values (3, 0, 2000, 1, to_date('01.08.2015','DD.MM.YYYY'));
insert into records values (4, 0, 3000, 1, to_date('01.09.2015','DD.MM.YYYY'));
insert into records values (5, 1, 5000, 1, to_date('01.10.2015','DD.MM.YYYY'));
insert into records values (6, 0, 3000, 1, to_date('01.10.2015','DD.MM.YYYY'));
insert into records values (7, 0, 10000, 2, to_date('01.08.2017','DD.MM.YYYY'));
insert into records values (8, 1, 1000, 2, to_date('05.08.2017','DD.MM.YYYY'));
insert into records values (9, 1, 2000, 2, to_date('21.09.2017','DD.MM.YYYY'));
insert into records values (10, 1, 5000, 2, to_date('24.10.2017','DD.MM.YYYY'));
insert into records values (11, 0, 6000, 2, to_date('26.11.2017','DD.MM.YYYY'));
insert into records values (12, 0, 120000, 3, to_date('08.09.2017','DD.MM.YYYY'));
insert into records values (13, 1, 1000, 3, to_date('05.10.2017','DD.MM.YYYY'));
insert into records values (14, 1, 2000, 3, to_date('21.10.2017','DD.MM.YYYY'));
insert into records values (15, 1, 5000, 3, to_date('24.10.2017','DD.MM.YYYY'));
insert into records values (16, 0, 50000, 4, to_date('02.08.2015','DD.MM.YYYY'));
insert into records values (17, 0, 25000, 4, to_date('02.09.2015','DD.MM.YYYY'));
insert into records values (18, 0, 25000, 4, to_date('03.10.2015','DD.MM.YYYY'));
insert into records values (19, 0, 50000, 4, to_date('30.11.2015','DD.MM.YYYY'));
insert into records values (20, 0, 132000, 5, to_date('01.10.2016','DD.MM.YYYY'));
insert into records values (21, 1, 650000, 6, to_date('01.12.2018','DD.MM.YYYY'));
insert into records values (22, 0, 1300000, 7, to_date('30.12.2024','DD.MM.YYYY'));
insert into records values (23, 0, 5000, 8, to_date('30.12.2024','DD.MM.YYYY'));
SELECT acc_num
FROM clients
INNER JOIN accounts ON clients.id = accounts.client_ref
INNER JOIN products ON products.client_ref = clients.id
INNER JOIN product_type ON products.product_type_id=product_type.id
WHERE product_type.name in ('ДЕПОЗИТ','КАРТА');
SELECT oper_date, case dt
when 1 then 'Кредит' else 'Дебет' end as TypeD, product_type.name, COALESCE(SUM(SUM),0) AS Summa
FROM records
INNER JOIN accounts ON accounts.id=records.acc_ref
INNER JOIN products ON accounts.product_ref=products.id
INNER JOIN product_type ON product_type.id = products.product_type_id
group by oper_date, dt, product_type.name
order by oper_date;
SELECT oper_date, product_type.name, COALESCE(SUM(SUM),0) AS Summa
FROM records
INNER JOIN accounts ON accounts.id=records.acc_ref
INNER JOIN products ON accounts.product_ref=products.id
INNER JOIN product_type ON product_type.id = products.product_type_id
group by oper_date, product_type.name
order by oper_date;