Re: Crosstab Confusion

From: Adam Sherman <adam(at)sherman(dot)ca>
To: Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Crosstab Confusion
Date: 2010-02-01 20:11:22
Message-ID: 26D00B1B-5C3D-4E4B-8350-9C8BE8456F97@sherman.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 2010-02-01, at 14:22 , Lee Hachadoorian wrote:
> The output column data type (day1, day2, etc.) is supposed to match the value data type. I used numeric(10,4) because that's what your original post specified, but the billed_duration column in your most recent post looks like it might be integer? (Or is it defined as numeric(10,4), but you never enter noninteger values?)

Actually, the query I was running is:

SELECT
cust_id as customer,
date_trunc(''day'', date) AS day,
SUM(billed_duration)/60.0::numeric(10,4) AS minutes

billed_duration is an integer. Make sense?

> What's the output of the category query by itself? I forgot to include ORDER BY 1 at the end of the category query. (The order should match the order of output columns, but I think without it you wouldn't get NULL values, just the values would be in the wrong columns.) I assume day_of_month has only one column, but I would suggest naming it explicitly instead of using *. And is the day_of_month column defined in the same format as date_trunc('day', date)? They must successfully pass an "equals" test to get included in the right crosstab cell. If a category value in the source query doesn't match any value produced by the category query, I think the crosstab function just throws out that row, which could lead to a table with the correct structure but all NULLs.

Right, my list of columns weren't equal to the truncated date. Using your suggested query to generate the columns fixed the problem!

Now, is there a way to generate the labels? Otherwise I have to adjust the query for th number of days returned.

Such nice output though! Awesome!

Thanks,

A.

--
www.sherman.ca / +1-613-797-6819 / +1-646-233-3400

"When the burning husks of your startups warm the last of your bones, remember I told you so." - Zed

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Scott Marlowe 2010-02-01 20:25:41 Re: combine SQL SELECT statements into one
Previous Message Lee Hachadoorian 2010-02-01 19:22:47 Re: Crosstab Confusion