Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
CREATE TABLE t ( product char, position char, count INT ); INSERT into t values ('onion', 'in', 10), ('onion', 'out', 3), ('cucumber', 'in', 30), ('carrot', 'in', 23), ('onion', 'out', 7), ('cucumber', 'out', 12), ('potato', 'in', 80), ('onion', 'in', 21), ('cabbage', 'in', 35), ('carrot', 'out', 12), ('cabbage', 'out', 3), ('mango', 'in', 25), ('onion', 'out', 3), ('potato', 'out', 10), ('apple', 'in', 45), ('cabbage', 'out', 20), ('cucumber', 'in', 81), ('apple', 'out', 7), ('mango', 'in', 13), ('orange', 'in', 75), ('orange', 'out', 75); SELECT product, SUM(count * (CASE WHEN position = 'in' THEN 1 ELSE -1 END)) AS result FROM t GROUP BY product HAVING result != 0;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear