R
R
Rom Black2016-08-19 13:57:31
MySQL
Rom Black, 2016-08-19 13:57:31

How to make a pivot table correctly?

Tell me how to correctly compose a query / pivot table
From table e, we obtain data with the following query.
select plid, oid, sum(e.show) from e group by plid, oid;
+----------+----------+-------------------+
| plid | oid | sum(e.show) |
+----------+----------+-------------------+
| 42 | 0 | 0 |
| 42 | 396 | 522 |
| 42 | 524 | 3 |
| 42 | 526 | 2 |
| 42 | 532 | 24 |
| 42 | 533 | 95 |
| 42 | 551 | 738 |
| 42 | 570 | 763 |
| 42 | 578 | 1 |
| 42 | 579 | 164 |
| 42 | 580 | 7 |
| 42 | 581 | 12 |
| 42 | 583 | 22 |
| 42 | 588 | 1 |
| 45 | 0 | 0 |
| 45 | 396 | 7 |
| 45 | 524 | 6 |
| 45 | 526 | 62 |
| 45 | 527 | 49 |
| 45 | 532 | 10 |
| 45 | 533 | 141 |
| 45 | 536 | 352 |
| 45 | 540 | 285 |
| 45 | 541 | 173 |
| 45 | 544 | 1598 |
| 45 | 551 | 361 |
| 45 | 577 | 284 |
| 45 | 578 | 138 |
| 45 | 579 | 98 |
| 45 | 580 | 170 |
| 45 | 581 | 8 |
| 45 | 583 | 14 |
| 45 | 588 | 216 |
| 47 | 0 | 0 |
| 47 | 396 | 9 |
| 47 | 407 | 328 |
| 47 | 532 | 501 |
| 47 | 555 | 0 |
| 47 | 563 | 868 |
| 47 | 588 | 22 |
+----------+----------+-------------------+
Need to make an oid X plid matrix
Quantity and the values ​​of oid and plid are not known in advance. At the intersection, the sum must be calculated.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question