-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple_PIVOTING_data.sql
More file actions
40 lines (31 loc) · 1.12 KB
/
Simple_PIVOTING_data.sql
File metadata and controls
40 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
or this challenge you need to PIVOT data. You have two tables, products and details. Your task is to pivot the rows in products to produce a table of products which have rows of their detail. Group and Order by the name of the Product.
Tables and relationship below:
You must use the CROSSTAB statement to create a table that has the schema as below:
CROSSTAB table.
name
good
ok
bad
Compare your table to the expected table to view the expected results.
CREATE EXTENSION tablefunc;
-- Create your CROSSTAB statement here
SELECT *
FROM CROSSTAB(
'SELECT A.NAME,B.DETAIL,COUNT(B.*) ct
FROM PRODUCTS A, DETAILS B
WHERE A.ID = B.PRODUCT_ID
GROUP BY 1,2
ORDER BY 1,2'
,$$VALUES ('good'::text), ('ok'::text),('bad'::text)$$)
AS ct (NAME text, good int, ok int, bad int);
select *
from crosstab(
'select p.name, d.detail, count(1)::int as value
from products p
inner join details d
on p.id = d.product_id
group by 1, 2
order by 1, 2',
$$values ('good'), ('ok'), ('bad')$$)
as (name text, good int, ok int, bad int)
order by name