pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adam Tauno Williams <awilliam@whitemice.org>
To: pgsql-general@postgresql.org
Subject: CROSSTAB( .. only one column has values... )
Date: Tue, 05 Jan 2021 10:46:08 -0500
Message-ID: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org> (raw)

I'm using the crosstab feature and do not understand why I am only
getting values in the first column.

The query:
SELECT 
  date_trunc('month', t2.value_date) AS invoice_date, 
  t1.value_string AS invoice_type
  COUNT(*)
FROM document d
  LEFT OUTER JOIN obj_property t1
    ON (t1.obj_id = d.document_id
        AND t1.namespace_prefix = 'http://www.example.com/ctabs';
        AND t1.value_key = 'invoiceType')
  LEFT OUTER JOIN obj_property t2
    ON (t2.obj_id = d.document_id
        AND t2.namespace_prefix = 'http://www.example.com/ctabs';
        AND t2.value_key = 'invoiceDate')
WHERE ((d.project_id = 1134510213) AND (d.is_folder = 0))
GROUP BY 1,2

 - has results like -

invoice_date invoice_type count 
------------ ------------ ----- 
2013-02      service      3454  
2013-03      service      3512  
2013-03      parts        5366  
2013-04      parts        5657  
2013-04      service      4612  
2013-05      service      4946  
2013-05      parts        5508  
...

So I put this in as a crosstab:

SELECT * FROM crosstab(
    $$
SELECT 
  SUBSTR(t2.value_string, 1, 7) AS invoice_date,
  t1.value_string AS invoice_type,
  COUNT(*)
FROM document d
  LEFT OUTER JOIN obj_property t1
    ON (t1.obj_id = d.document_id
        AND t1.namespace_prefix = 'http://www.example.com/ctabs';
        AND t1.value_key = 'invoiceType')
  LEFT OUTER JOIN obj_property t2
    ON (t2.obj_id = d.document_id
        AND t2.namespace_prefix = 'http://www.example.com/ctabs';
        AND t2.value_key = 'invoiceDate')
WHERE ((d.project_id = 1134510213) AND (d.is_folder = 0))
GROUP BY 1,2
 $$) AS final_result (invoice_date VARCHAR, "parts" BIGINT, "rental"
BIGINT, "sales" BIGINT, "service" BIGINT);

 - and I get the results of -

invoice_date parts rental sales  service 
------------ ----- ------ ------ ------- 
2001-09      1     (null) (null) (null)  
2007-07      1     (null) (null) (null)  
2013-02      5353  (null) (null) (null)  
2013-02      3454  (null) (null) (null)  
2013-03      3512  (null) (null) (null)  
2013-03      5366  (null) (null) (null)  
...

Only the first column has values, all the rest are NULL.
I assume I am missing something stupid.

-- 
Adam Tauno Williams <mailto:awilliam@whitemice.org> GPG D95ED383
Systems Administrator, Python Developer, LPI / NCLA






view thread (9+ messages)  latest in thread

Message-ID: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org>
Permalink:  ../39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org/
Also on:    postgresql.org/message-id/39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: awilliam@whitemice.org
  Subject: Re: CROSSTAB( .. only one column has values... )
  In-Reply-To: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org>
  References: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox