Advertisement
elena1234

convert data from rows into columns in MySQL

Feb 12th, 2023
1,684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.39 KB | Source Code | 0 0
  1. SELECT
  2. product_name,
  3. SUM(CASE
  4.   WHEN store_location = 'North' THEN num_sales ELSE 0 END
  5. ) AS north,
  6. SUM(CASE
  7.   WHEN store_location = 'Central' THEN num_sales ELSE 0 END
  8. ) AS central,
  9. SUM(CASE
  10.   WHEN store_location = 'South' THEN num_sales ELSE 0 END
  11. ) AS south,
  12. SUM(CASE
  13.   WHEN store_location = 'West' THEN num_sales ELSE 0 END
  14. ) AS west
  15. FROM product_sales
  16. GROUP BY product_name
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement