1

My Postgres crosstab query reads:

SELECT mthreport.*
FROM crosstab
('SELECT 
to_char(ipstimestamp, ''mon DD HH24h'') As row_name, 
varid::text || log.varid || ''_'' || ips.objectname::text As bucket, 
COUNT(*)::integer As bucketvalue
FROM loggingdb_ips_boolean As log 
INNER JOIN IpsObjects As ips 
ON log.Varid=ips.ObjectId
GROUP BY to_char(ipstimestamp, ''yyyy MM DD HH24h''), row_name, bucket 
ORDER BY to_char(ipstimestamp, ''yyyy MM DD HH24h''), row_name, bucket')
As mthreport(item_name text,
jan3 integer, feb4 integer, mar5 integer)

Is there any way that I can pull the three items enumerated in the last line (jan3 integer etc.) from an ancillary query? I tried to substitute the last line with SELECT xyz FROM zyx, but that doesn't work.

1
  • Do you have your answer? Commented Jan 18, 2017 at 22:30

1 Answer 1

2

In the basic form of the query, you have to spell out the column names and types in the calling SELECT.

There are the crosstabN(text) variants that use a pre-defined return types.

For full automation you'll have to wrap each individual query into a function where you can predefine the return type. I posted a detailed example here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.