I have a table A :
-----------------------------
| id | nombre | cel |
-----------------------------
|1 | Juan | 1653727|
|2 | Pedro | 6353526|
|3 | Hernan | 4465672|
-----------------------------
Another table B :
--------------------
| id | material |
--------------------
|1 | lapiz |
|2 | cuaderno |
|3 | mochila |
--------------------
And an intemedia table AB :
-----------------------------
| id_A | id_B | cantidad |
-----------------------------
|1 | 1 | 3 |
|1 | 2 | 2 |
|2 | 1 | 2 |
|2 | 2 | 2 |
|3 | 1 | 4 |
|3 | 2 | 1 |
|3 | 3 | 1 |
-----------------------------
And I have a query like this:
SELECT
A.nombre nombre,
B.material material,
AB.cantidad cantidad
FROM AB
INNER JOIN A ON AB.id_A = A.id
INNER JOIN B ON AB.id_B = B.id
And the result is the following:
----------------------------------
| nombre | material | cantidad |
----------------------------------
| Juan | lapiz | 3 |
| Juan | cuaderno | 2 |
| Pedro | lapiz | 2 |
| Pedro | cuaderno | 2 |
| Hernan | lapiz | 4 |
| Hernan | cuaderno | 1 |
| Hernan | mochila | 1 |
----------------------------------
The problem is that I have repeated names and I do not want them that way.
The format I want is the following:
------------------------------------------------
| nombre | lapiz | cuaderno | mochila |
------------------------------------------------
| Juan | 3 | 2 | 0 |
| Pedro | 2 | 2 | 0 |
| Hernan | 4 | 1 | 1 |
------------------------------------------------
I do not know if it is possible to do this with a query, function or stored procedure in postgres
.
I appreciate any help you can give me.