dimanche 24 mai 2015

SQLite Excel style Pivot table

I am using SQLite as my database in my C++ application. I have a table similar to one given below

Day | PartyA | PartyB | Amount
--------------------------------
1   |    X   |   Y    | 25
1   |    X   |   K    | 10
1   |    Y   |   M    | 30
1   |    Z   |   L    | 20
2   |    X   |   Y    | 10
2   |    X   |   K    | 30
2   |    Y   |   M    | 50
2   |    Z   |   L    | 5
3   |    X   |   K    | 60
3   |    Y   |   M    | 15

and I want to get something like the table below. This is similar to MS Excel pivot table; the final table has unique PartyA - PartyB pairs and a column named TotalAmount has the summation of Amount values given in the previous table.

PartyA | PartyB | TotalAmount
--------------------------------
   X   |   Y    | 35
   X   |   K    | 100
   Y   |   M    | 95
   Z   |   L    | 25

Although, the C/C++ API interface allows to implement this, I want to do it by using SQL queries.

Your assistance will be much appreciated on the matter. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire