dbTalk Databases Forums  

Automizing crosstab queries

comp.databases.postgresql comp.databases.postgresql


Discuss Automizing crosstab queries in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Julia Jacobson
 
Posts: n/a

Default Automizing crosstab queries - 12-17-2010 , 04:09 AM






Dear PostgreSQL experts,

Is it possible to automize crosstab queries on a normalized database in
PostgreSQL?
A minimal example is a table with the results of students in different
exams:

student | date_of_exam | grade
------------------------------
Peter | 2010-09-09 | 2
Tom | 2010-09-09 | 1
Andy | 2010-09-21 | 3
Tom | 2010-09-21 | 4
Peter | 2010-09-21 | 1
Peter | 2010-10-11 | 2

This can be transformed to a denormalized view like:

student | grade_2010_09_09 | grade_2010_09_21 | grade_2010_10_11
----------------------------------------------------------------
Peter | 2 | 1 | 2
Tom | 1 | 4 | NULL
Andy | NULL | 3 | NULL

However, each time the results of a new exam are inserted, the view has
to be adjusted by hand.
Is there a way of doing this automatically?

Thanks in advance,
Julia

Reply With Quote
  #2  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Automizing crosstab queries - 12-17-2010 , 05:36 AM






Julia Jacobson, 17.12.2010 11:09:
Quote:
Dear PostgreSQL experts,

Is it possible to automize crosstab queries on a normalized database in PostgreSQL?
A minimal example is a table with the results of students in different exams:

student | date_of_exam | grade
------------------------------
Peter | 2010-09-09 | 2
Tom | 2010-09-09 | 1
Andy | 2010-09-21 | 3
Tom | 2010-09-21 | 4
Peter | 2010-09-21 | 1
Peter | 2010-10-11 | 2

This can be transformed to a denormalized view like:

student | grade_2010_09_09 | grade_2010_09_21 | grade_2010_10_11
----------------------------------------------------------------
Peter | 2 | 1 | 2
Tom | 1 | 4 | NULL
Andy | NULL | 3 | NULL

However, each time the results of a new exam are inserted, the view has to be adjusted by hand.
Is there a way of doing this automatically?

Check out the crosstab function in the "tablefunc" module. It's not perfect but seems to work

Regards
Thomas

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.