Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE solicitations
- (
- id SERIAL PRIMARY KEY,
- name text
- );
- CREATE TABLE donations
- (
- id SERIAL PRIMARY KEY,
- solicitation_id integer REFERENCES solicitations, -- can be null
- created_at timestamp without time zone NOT NULL DEFAULT (now() at time zone 'utc'),
- amount bigint NOT NULL DEFAULT 0
- );
- INSERT INTO solicitations (name) VALUES
- ('solicitation1'), ('solicitation2');
- INSERT INTO donations (solicitation_id, amount) VALUES
- (null, 10), (1, 20), (2, 30);
- SELECT
- "created_at"
- , COALESCE("no_solicitation", 0) AS "no_solicitation"
- , COALESCE("1", 0) AS "1"
- , COALESCE("2", 0) AS "2"
- FROM crosstab(
- $source_sql$
- SELECT
- created_at::date as row_id
- , COALESCE(solicitation_id::text, 'no_solicitation') as category
- , SUM(amount) as value
- FROM donations
- GROUP BY row_id, category
- ORDER BY row_id, category
- $source_sql$
- , $category_sql$
- SELECT 'no_solicitation' AS foo
- UNION ALL
- SELECT DISTINCT id::text AS foo FROM solicitations ORDER BY foo
- $category_sql$
- ) AS ct (
- "created_at" date
- , "no_solicitation" bigint
- , "1" bigint
- , "2" bigint
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement