Thread: How can I correctly handle sparse crosstabs?

How can I correctly handle sparse crosstabs?

From
Rob Richardson
Date:

It seems that crosstab queries assume there is going to be a value for every possible row in the source query.  That’s not what I have, and I am not getting the result I need.

 

Here is the definition of my source table:

CREATE TABLE plc_values

(

  plc_values_key bigserial NOT NULL,

  recorded_date timestamp with time zone,

  tag_name character varying(120),

  tag_value character varying(120),

  CONSTRAINT plc_values_pk PRIMARY KEY (plc_values_key )

)

 

There are 30 possible tag_name values.  But for a given recorded_date, there may only be one tag, and the most that I see is five tags.  When I run the crosstag query, the data columns are filled in from left to right, regardless of the name of the column. 

 

Here is the query:

select * from

crosstab($$ select recorded_date, tag_name, tag_value from plc_values$$ || 

                 $$ order by recorded_date, tag_name $$ )

as ct(

read_time timestamp with time zone,

Crane2_Crane_Loaded character varying(120),

Crane2_Crane_LoadWeight character varying(120),

<28 more fields snipped>

)

 

I always get a value in the Crane2_Crane_Loaded field.  It may not have come from that tag, though.  Sometimes the value is “True” or “False”, or maybe 1 or 0, or maybe even 102598. 

 

If crane2_crane_loadweight has a value, it is guaranteed that crane2_crane_loaded has a value.  In general, if a column has a value, then the column to its left has a value. 

 

There is no relation between the value in the column and the tag name the value was associated with in the source table.  How can I enforce that relationship in a crosstab?  Or am I plain out of luck?

 

Thanks very much!

 

RobR

 

Re: How can I correctly handle sparse crosstabs?

From
Joe Conway
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/26/2014 07:56 AM, Rob Richardson wrote:
> It seems that crosstab queries assume there is going to be a value
> for every possible row in the source query.  That?s not what I
> have, and I am not getting the result I need.

<snip>

> There is no relation between the value in the column and the tag
> name the value was associated with in the source table.  How can I
> enforce that relationship in a crosstab?  Or am I plain out of
> luck?

Please see:
http://www.postgresql.org/docs/9.3/interactive/tablefunc.html

In particular the examples about 2/3rds down the page.

HTH,

Joe

- --
Joe Conway
credativ LLC: http://www.credativ.us
Linux, PostgreSQL, and general Open Source
Training, Service, Consulting, & 24x7 Support
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJTDhNeAAoJEDfy90M199hlC5EQAKeooqaU2MeWhJehYwv07Z0T
7vBK781wbrDZaflGCdFauIl+yPxZUjPQUob7+Fzo7CFFtMXC1Sxx762/gp21U2EH
jmkn+PSfbmaSqmL22lr9IKbxsLmXgGNucO3CauwN+f7f09cd8mdptr6TS1R1+kSE
TPMFiJH8nnl1YQ7Z11hNcx/yG6Ad0ihdtyvCX+5DbEIYt6z5TK6tX7KG+YBLTCZN
hRW4WNb3dRYUnR0JFSbCqz9IBRfwL48/PkrzUO/o1i5jHmL19z/36GNJ9ey6rd3e
aqznoa6nUKshisjuv7DuZn1ZpQsZ+tqzJsQEzF8OVdSN/yC0w9740y5ytzcDHBuD
fd5LZfOePhLZqeNfjnFGJeRiWighTmktMO73Jl5EvGzZQFKvPp2+ynk6fp8hLxgW
VtNA7lxLKJEnUWehdgeYrUIX61ukzkBRu27MQrIKV0WgsPk9V6YJ80s09kB5Dvr3
YrrrzNyCi7dP361VO1VF383JXyC4P1ZH5Y2baVXDVKffwVAsBRT3oPhEbEsGzg8m
U2vxQlqQfuLUkaXHibm0F/B/Om7zM1yXi4cKi4W5jv81/6kIe0kz2jGbgI5rEPaB
ylBfzM0vR55xmTDPfmxTnu8tBzrV3Z43aLe7SVQfOeX3EIOTFj2896e0ZnAoyKyb
F2aDdiZBwDO7C2IdhTLJ
=riQs
-----END PGP SIGNATURE-----