SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
WITH customers AS ( SELECT 1 customer_id UNION ALL SELECT 2 customer_id ), orders AS ( SELECT 1 order_id, 1 customer_id, '2024-09-01'::DATE dateorder, 100 sum_delivery UNION ALL SELECT 2 order_id, 1 customer_id, '2024-09-02'::DATE dateorder, 0 sum_delivery UNION ALL SELECT 3 order_id, 2 customer_id, '2024-09-01'::DATE dateorder, 0 sum_delivery ) SELECT c.customer_id FROM customers c INNER JOIN LATERAL ( SELECT o.order_id, o.customer_id, COALESCE(o.sum_delivery, 0) sum_delivery FROM orders o WHERE o.customer_id = c.customer_id AND o.dateorder >= '20240830' ORDER BY o.order_id LIMIT 1 ) AS o ON o.sum_delivery = 0;

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear