SQL crosstab

Questions and answers regarding general SQL and backend databases

SQL crosstab

Postby jdbruijn » Fri Jan 29, 2016 1:21 pm

For use in a jasper report I need to transpose the data from the following (simplified) query:
Code: Select all
select process_name, count(id), sum(done)
from myTable
group by process_name

Where I don't know how many processes there are
This is the result of that query
Code: Select all
process_name | count | sum
-------------|-------|----
process_1    |    20 |  10
process_2    |    30 |   9
process_3    |    11 |   9
....
process_N    |     x |   x

For use in the report I need it in the following format:
Code: Select all
      | process_1 | process_2 | process_3 | ... | process_N
-----------------------------------------------------------
count |        20 |        30 |        11 |     |         x
sum   |        10 |         9 |         9 |     |         y


I've been looking at postgresql crosstab function, but that requires a knowledge of the nr of resulting columns. Is there anyother solution to this?
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: SQL crosstab

Postby Bernd.N » Fri Jan 29, 2016 9:49 pm

It might be I do not see the real task, but after your initial SELECT you do have the nr of the resulting columns, or don't you?
As then you can put together the needed SQL statement in a string and fire it with plugins.rawSQL.executeSQL( or any Servoy statement that does this too), creating the (temporary) table you need.

If there were no crosstab function, you could also create the second table from scratch with a loop and some SQL statements you put together.
Bernd Korthaus
LinkedIn
Servoy 7.4.9 SC postgreSQL 9.4.11 Windows 10 Pro
User avatar
Bernd.N
 
Posts: 544
Joined: Mon Oct 21, 2013 5:57 pm
Location: Langenhorn, North Friesland, Germany

Re: SQL crosstab

Postby erdione » Sun Jan 31, 2016 9:17 am

what don't you simply use jasper cross table component?
Fabrice
erdione
 
Posts: 112
Joined: Thu Feb 01, 2007 1:19 pm

Re: SQL crosstab

Postby jdbruijn » Mon Feb 01, 2016 10:16 am

erdione wrote:what don't you simply use jasper cross table component?

I'm looking into that at the moment, but I cannot find how I can remove the totals row (and keep the totals column)
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm


Return to SQL Databases

Who is online

Users browsing this forum: No registered users and 4 guests