pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: awilliam@whitemice.org
To: pgsql-general@postgresql.org
Subject: Re: CROSSTAB( .. only one column has values... )
Date: Tue, 5 Jan 2021 13:53:56 -0500
Message-ID: <78c8bfc6-754e-5899-a37d-9e40e7758037@joeconway.com> (raw)
In-Reply-To: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org>
References: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org>

On 1/5/21 10:46 AM, Adam Tauno Williams wrote:
> I'm using the crosstab feature and do not understand why I am only
> getting values in the first column.

<snip>

> So I put this in as a crosstab:
> 
> SELECT * FROM crosstab(
>     $$
 <snip>

>  $$) 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.

Seems you are using the wrong form of the crosstab() function. See

  https://www.postgresql.org/docs/current/tablefunc.html#id-1.11.7.47.5.7.2

"The main limitation of the single-parameter form of crosstab is that it treats
all values in a group alike, inserting each value into the first available
column. If you want the value columns to correspond to specific categories of
data, and some groups might not have data for some of the categories, that
doesn't work well. The two-parameter form of crosstab handles this case by
providing an explicit list of the categories corresponding to the output columns."

HTH,

Joe

-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (9+ messages)  latest in thread

Message-ID: <78c8bfc6-754e-5899-a37d-9e40e7758037@joeconway.com>
Permalink:  ../78c8bfc6-754e-5899-a37d-9e40e7758037@joeconway.com/
Also on:    postgresql.org/message-id/78c8bfc6-754e-5899-a37d-9e40e7758037@joeconway.com

 · 

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: mail@joeconway.com, awilliam@whitemice.org
  Subject: Re: CROSSTAB( .. only one column has values... )
  In-Reply-To: <78c8bfc6-754e-5899-a37d-9e40e7758037@joeconway.com>
  References: <39d8f6180ae91806f6d6738ccb761b4b615e404c.camel@whitemice.org> <78c8bfc6-754e-5899-a37d-9e40e7758037@joeconway.com>

* 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