'Trying to calculate on each day how many products were sold accross all departments
DO
$$BEGIN
EXECUTE FORMAT('create table prouduct_pivots AS ' ||
'SELECT * FROM CROSSTABLE(%L, %L) AS ct(day date, %s)',
'SELECT DATE_TRUNC(''day'', s.transation_date) AS day, product.name, count(*) cnt FROM sale s, product p WHERE s.product_id = product.id GROUP BY DATE_TRUNC(''day'', sale.transation_date), product.name
ORDER BY 1'
'SELECT DISTINCT name FROM product ORDER BY 1',
(SELECT STRING_AGG(replace(name, '', '_') || 'text', ',') FROM product ORDER BY 1)
);
END$$;
SELECT * FROM product_pivot;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
