SQL exercise · about 5 minutes · free
Pivot with Conditional Sums
Managers want one row per player and one column per product. SQL can pivot without any special syntax.
Conditional aggregation puts a CASE inside an aggregate: SUM(CASE WHEN item = 'skin' THEN amount ELSE 0 END) adds up only skin sales. One expression per column gives you a pivot table.
Your quest: For each
player_id, show total spend on skin, pass and boost as three columns with those names.Database:
players(id, name, class, level, country) · matches(id, player_id, map, kills, deaths, score) · orders(id, player_id, order_date, item, amount) · app_events(user_id, event, day) · employees(id, name, dept, salary, manager_id, hire_date) · logins(user_id, day) · payment_events(payment_id, user_id, amount, status, updated_at)Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
SUM(CASE WHEN item = 'skin' THEN amount ELSE 0 END) AS skin, ... GROUP BY player_idOutput appears here.