SQLize Online / PHPize Online

A A A
Share   Donate   Blog   Popular
Copy Format Clear
create table claims ( claim_id integer PRIMARY KEY, delo_id integer, reg_num text, reg_date timestamp, category text ); create table delo (delo_id integer PRIMARY KEY, straf integer); INSERT INTO delo VALUES (1, 100), (2, 250), (3, 300), (4, 250), (5, 300) ; INSERT INTO claims VALUES (1,1,'77-08', '2022-05-01 00:00:00', 'инспектору'), (2,2,'77-08', '2022-05-02 00:00:00', 'инспектору'), (3,2,'228', '2022-05-02 00:00:05', 'Судебная'), (4,3,'77-08', '2022-05-03 00:00:00', 'инспектору'), (5,3,'228', '2022-05-03 00:00:00', 'инспектору'), (6,4,'77-08', '2022-05-03 00:00:00', 'инспектору'), (7,4,'77-08', '2022-05-03 00:00:50', 'инспектору'), (8,4,'77-08', '2022-05-03 00:01:50', 'Судебная'), (9,4,'77-08', '2022-05-03 00:5:50', 'инспектору'), (10,5,'77-08', '2022-05-03 00:08:00', 'инспектору'), (11,5,'77-08', '2022-05-03 00:07:00', 'инспектору'), (12,5,'77-08', '2022-05-03 00:10:00', 'Судебная'), (13,5,'77-08', '2022-05-03 00:06:00', 'инспектору'); SELECT * FROM claims; SELECT a.*, b.max_date FROM claims a left join ( SELECT delo_id, max(reg_date) max_date FROM claims group by delo_id ) b on a.delo_id = b.delo_id and a.reg_date =b. max_date left join ( select distinct delo_id from claims where reg_num = '77-08' ) c on a.delo_id = c.delo_id where a.category = 'Судебная' and b.max_date is not null and c.delo_id is not null ;
Stuck with a problem? Need help? Ask professionals on our Telegram channel!
Join the channel
Your quersion is posted.
Join our Telegram channel and get answers.
Send
Copy