# Thread: Is it Possible with a Complex Query?

1. Registered User
Join Date
Dec 2012
Posts
34

## Unanswered: Is it Possible with a Complex Query?

Hi to All,

Excuse for the perhaps hard question.

I have a table "invoice_lines" of the form:
Code:
```Year   |week	|item	|matter 	|total|
+        +       +
2012   |1  	|book_1 |Philosopy      |37,50|
2012   |1  	|book_1 |Philosopy      |37,50|
2012   |1  	|book_2 |Story          |47,50|
2012   |1  	|book_2 |Story          |47,50|
2012   |2  	|book_1 |Philosopy      |37,50|
2012   |2  	|book_2 |Story          |47,50|
2012   |2  	|book_2 |Story          |47,50|
2012   |3  	|book_1 |Philosopy      |37,50|
2012   |3  	|book_1 |Philosopy      |37,50|
2012   |3  	|book_1 |Philosopy      |37,50|
2012   |3  	|book_2 |Story          |47,50|
2012   |3  	|book_2 |Story          |47,50|
2012   |3  	|book_2 |Story          |47,50|
2012   |4  	|book_2 |Story          |47,50|```
Strating from this table, I'd want arrive to obtain a view as:

Code:
```Year |    matter   |total_week1 |total_week2  |total_week3  |total_week4|... total_week53|
+             +            +             +             +
2012 |    Philosopy|      75,00 |       37,50 |       112,50|          0|               0|
2012 |        Story|      95,00 |       95,00 |       142,50|      47,50|               0|```
Could SomeOne tell if is it possible?

If yes, trough which query?

Gianni Pinna.

2. Registered User
Join Date
Nov 2003
Posts
2,988
This is called "pivot" and is not something that relation databases are good at. There is the "tablefunc" module Postgres that gives you a "crosstab" function for precisely this requirement.

PostgreSQL: Documentation: 9.2: tablefunc

3. Registered User
Join Date
Dec 2012
Posts
34
Hi Shammat,

tank You for Your promt replay, with wich You immediately clarify the limits.