Re: CROSSTAB( .. only one column has values... )

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: awilliam(at)whitemice(dot)org
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: CROSSTAB( .. only one column has values... )
Date: 2021-01-05 16:12:47
Message-ID: CAKFQuwZDC4rqyRWA42EHqDjZroNagvjw+MnqPi5gL8fzQeOQzg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jan 5, 2021 at 8:46 AM Adam Tauno Williams <awilliam(at)whitemice(dot)org>
wrote:

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

I think you are assigning the function more intelligence/effort than it
puts out.

From the documentation:
"""
The crosstab function produces one output row for each consecutive group of
input rows with the same row_name value. It fills the output value columns,
left to right, with the value fields from these rows. If there are fewer
rows in a group than there are output value columns, the extra output
columns are filled with nulls; if there are more rows, the extra input rows
are skipped.

In practice the SQL query should always specify ORDER BY 1,2 to ensure that
the input rows are properly ordered, that is, values with the same row_name
are brought together and correctly ordered within the row. Notice that
crosstab itself does not pay any attention to the second column of the
query result; it's just there to be ordered by, to control the order in
which the third-column values appear across the page.
"""

The fact you don't have an order by, and that there are not an equal number
of records per date, suggests to me that you are expecting the function to
fill in the blanks when the documentation says it doesn't do that.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tim Clarke 2021-01-05 17:11:37 Re: CROSSTAB( .. only one column has values... )
Previous Message Adam Tauno Williams 2021-01-05 15:46:08 CROSSTAB( .. only one column has values... )