SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Hint: use Ctrl+Enter for SQL autocomplete create table order_items ( id int primary key, price decimal(9,2), quantity int ); create table waybills_items ( order_item_id int primary key, quantity int ); create table acts_items ( order_item_id int primary key, quantity int ); insert into order_items values (1, 1.23, 5), (2, 3.45, 7), (3, 6.78, 12), (4, 9.01, 12); insert into waybills_items values (1, 2), (2,5); insert into acts_items values (2,3), (3,5); select oi.id, (oi.price * coalesce(wi.quantity, 0)) wi_sum, (oi.price * coalesce(ai.quantity, 0)) ai_sum, (oi.price * coalesce(least(wi.quantity, ai.quantity), 0)) both_sum from order_items oi left join waybills_items wi on wi.order_item_id = oi.id left join acts_items ai on ai.order_item_id = oi.id order by oi.id
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear