Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 879 Bytes

SQL-Basics-Simple-PIVOTING-data-WITHOUT-CROSSTAB.md

File metadata and controls

30 lines (22 loc) · 879 Bytes

This kata is inspired by SQL Basics: Simple PIVOTING data by matt c.

You need to build a pivot table WITHOUT using CROSSTAB function. Having two tables products and details you need to select a pivot table of products with counts of details occurrences (possible details values are ``['good', 'ok', 'bad']`.

Results should be ordered by product's name.

Model schema for the kata is:

your query should return table with next columns

  • name
  • good
  • ok
  • bad Compare your table to the expected table to view the expected results.
-- add your query here!
SELECT p.name,
  COUNT(CASE WHEN d.detail = 'good' THEN 1 END) as good,
  COUNT(CASE WHEN d.detail = 'ok'  THEN 1 END) as ok, 
  COUNT(CASE WHEN d.detail = 'bad' THEN 1 END) as bad
FROM products p
INNER JOIN details d ON p.id = d.product_id
GROUP BY p.name
ORDER BY p.name