add remove split language chart show hidden hide
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE
INSERT 0 27
format
SELECT * FROM crosstab(
   $q$
   SELECT to_char(date_trunc('month', creation_date), 'YYYY_Month') AS year_month
        , marking
        , COUNT(*) AS ct
   FROM invoices
   GROUP BY date_trunc('month', creation_date), marking
   ORDER BY date_trunc('month', creation_date) -- optional
   $q$
 , $c$VALUES ('Delivered'), ('Not Delivered'), ('Not Received')$c$
   ) AS ct(year_month text, "Delivered" int, "Not Delivered" int, "Not Received" int);
SELECT 1
year_month Delivered Not Delivered Not Received
2020_January 1 1 1
2020_March 2 2 null
2021_January 1 2 1
2021_February null null 1
2021_March 1 null null
2021_August 1 1 2
2022_August 2 null null
2022_November 2 3 1
2022_December null null 2
SELECT 9