-- create create table clnt_aggr ( client_dk integer, tb_name Varchar (100), salary integer, pos_amt integer, pos_qty integer, report_dt date, primary key(client_dk, tb_name, report_dt) ); -- insert INSERT INTO clnt_aggr VALUES (507851886, 'Московский банк', 15000, 6400, 8, '2019-05-31'), (267188214, 'Центрально-Черноземный банк', 40000, 13200, 10, '2019-06-30'), (148849526, 'Центрально-Черноземный банк', 40000, 35000, 3, '2019-05-31'), (148849526, 'Московский банк', 30000, 35000, 3, '2019-06-30'), (148849526, 'Московский банк', 31000, 35000, 3, '2019-07-30'), (148849526, 'Московский банк', 32000, 35000, 3, '2019-08-30'), (148849526, 'Московский банк', 33000, 35000, 3, '2019-09-30'), (148849526, 'Московский банк', 55000, 35000, 3, '2019-10-30'), (507851886, 'Московский банк', 12000, 6400, 8, '2019-06-30'), (507851886, 'Московский банк', 50000, 6400, 8, '2019-08-30'), (507851886, 'Московский банк', 35000, 6400, 8, '2019-09-30'), (507851886, 'Московский банк', 55000, 6400, 8, '2019-10-30'), (613898474, 'Юго-Западный банк', 150000, 61000, 26, '2019-06-30'); -- fetch --SELECT * FROM clnt_aggr; -- create create table clnt_data ( client_dk integer, actual_from_dt date, actual_to_dt date, gender Varchar(1), age integer, сhild_qty integer, CONSTRAINT code_title PRIMARY KEY(client_dk, actual_from_dt) ); -- insert INSERT INTO clnt_data VALUES (507851886, '2019-03-05', '2019-07-20', 'M', 25, 1), (507851886, '2019-07-21', '2099-12-31', 'M', 25, 2), (267188214, '2019-02-01', '2099-12-31', 'M', 46, 3), (148849526, '2018-09-15', '2099-12-31', 'W', 18, 0), (613898474, '2018-05-11', '2099-12-31', 'M', 73, 2); -- task 2: -- Определяю самую актуальную дату, как дату последнего репорта в тер.банке -- условие трактую так: отобрать записи с последними report_dt, среди них найти макс. зп/возраст для каждого тб -- как сделал -- с исп. подзапроса оставляю только строки на самую актуальную дату для каждого тб -- вывожу самую высокую зп/возраст. Если несколько возрастов получили одинаковую макс.зп, то выведу самого младшего select tb_name, age, salary from ( select a.report_dt, a.tb_name, b.age, a.salary, max(a.report_dt) over (partition by a.tb_name) as max_report_dt, max(a.salary) over (partition by a.tb_name, report_dt) as max_salary_by_report_dt FROM clnt_aggr a inner join clnt_data b on a.client_dk = b.client_dk and a.report_dt between b.actual_from_dt and b.actual_to_dt order by a.tb_name, a.client_dk, a.salary desc, a.report_dt desc ) a where report_dt = max_report_dt and salary = max_salary_by_report_dt
Write, Run & Share PostgreSQL queries online using OneCompiler's PostgreSQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for PostgreSQL. Getting started with the OneCompiler's PostgreSQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'PostgreSQL' and start writing queries to learn and test online without worrying about tedious process of installation.
PostgreSQL is a open source relational database system and is also knows as Postgres.
CREATE command is used to create a table, schema or an index.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
ALTER command is used to add, modify or delete columns or constraints from the database table.
ALTER TABLE Table_name ADD column_name datatype;
TRUNCATE command is used to delete the data present in the table but this will not delete the table.
TRUNCATE table table_name;
DROP command is used to delete the table along with its data.
DROP TABLE table_name;
RENAME command is used to rename the table name.
ALTER TABLE table_name1 RENAME to new_table_name1;
INSERT Statement is used to insert new records into the database table.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Select statement is used to select data from database tables.
SELECT column1, column2, ...
FROM table_name;
UPDATE statement is used to modify the existing values of records present in the database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE statement is used to delete the existing records present in the database table.
DELETE FROM table_name where condition;