Advertisement
cdsatrian

pivot table 20170314

Mar 14th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.83 KB | None | 0 0
  1. DROP TABLE `myTable`;
  2.  
  3. CREATE TABLE `myTable` (
  4.   `id` mediumint(8) unsigned NOT NULL auto_increment,
  5.   `bulan` mediumint default NULL,
  6.   `tahun` mediumint default NULL,
  7.   `harga` mediumint default NULL,
  8.   `idsub` mediumint default NULL,
  9.   PRIMARY KEY (`id`)
  10. ) AUTO_INCREMENT=1;
  11.  
  12. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (11,2016,23329,1),(6,2016,2626,1),(12,2016,84285,1),(5,2015,29293,1),(2,2017,86416,1),(11,2015,16946,2),(7,2015,98536,2),(12,2017,89962,2),(5,2017,57941,3),(7,2015,98555,1);
  13. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (4,2015,86540,1),(1,2016,46104,3),(5,2015,11445,2),(1,2015,76269,1),(2,2017,17722,3),(5,2016,64326,2),(7,2015,37585,2),(4,2015,3564,1),(2,2016,98076,1),(2,2016,47144,1);
  14. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (12,2016,96951,1),(4,2017,24639,2),(4,2016,98482,1),(9,2015,43258,3),(7,2016,53117,3),(2,2017,88848,2),(2,2016,36317,1),(1,2017,9281,3),(10,2017,40416,1),(1,2017,70073,2);
  15. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (3,2016,33005,3),(10,2016,69600,3),(1,2016,59163,1),(9,2015,83234,1),(4,2015,75896,2),(12,2016,13795,1),(11,2015,85896,1),(5,2015,73130,1),(8,2015,70736,3),(5,2016,59082,3);
  16. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (11,2015,45831,1),(2,2016,39817,1),(10,2015,33305,3),(1,2017,79125,3),(11,2016,83363,3),(2,2015,51376,1),(12,2015,9449,3),(9,2017,60803,2),(3,2015,56472,3),(2,2017,8102,3);
  17. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (5,2016,23511,1),(5,2017,41677,1),(4,2016,20010,2),(3,2016,65803,3),(11,2017,30179,3),(8,2017,54545,1),(6,2016,1948,3),(4,2016,69861,2),(10,2015,11295,3),(5,2015,77075,2);
  18. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (4,2017,59135,1),(2,2015,97578,1),(2,2017,66261,3),(6,2016,19360,2),(5,2016,17884,1),(6,2015,53823,3),(4,2017,98098,1),(2,2015,16478,3),(1,2015,10640,1),(4,2015,71217,2);
  19. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (6,2015,64194,3),(11,2015,7437,1),(4,2017,27444,2),(8,2016,61955,3),(12,2015,50782,3),(11,2015,32836,1),(9,2015,52889,2),(4,2017,82400,3),(1,2017,50439,2),(9,2016,78641,1);
  20. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (1,2016,94502,2),(6,2016,60564,1),(11,2016,66448,3),(11,2017,41518,2),(7,2015,61334,1),(10,2016,94337,2),(7,2017,72823,2),(11,2016,25731,2),(1,2017,93862,3),(12,2016,43049,3);
  21. INSERT INTO `myTable` (`bulan`,`tahun`,`harga`,`idsub`) VALUES (7,2015,59943,3),(8,2015,40994,3),(9,2017,48142,2),(9,2015,15950,3),(11,2016,79144,2),(12,2016,86359,1),(1,2015,40522,2),(6,2016,49389,1),(8,2017,50801,1),(8,2016,66492,3);
  22.  
  23. SELECT
  24. idsub,
  25. SUM(IF(bulan=1,harga,0)) AS jan,
  26. SUM(IF(bulan=2,harga,0)) AS feb,
  27. SUM(IF(bulan=3,harga,0)) AS mar,
  28. SUM(IF(bulan=4,harga,0)) AS apr,
  29. SUM(IF(bulan=5,harga,0)) AS mei,
  30. SUM(IF(bulan=6,harga,0)) AS jun,
  31. SUM(IF(bulan=7,harga,0)) AS jul,
  32. SUM(IF(bulan=8,harga,0)) AS agu,
  33. SUM(IF(bulan=9,harga,0)) AS sep,
  34. SUM(IF(bulan=10,harga,0)) AS okt,
  35. SUM(IF(bulan=11,harga,0)) AS nov,
  36. SUM(IF(bulan=12,harga,0)) AS des
  37. FROM myTable
  38. WHERE tahun=2016
  39. GROUP BY idsub;
  40.  
  41. +-------+-------+--------+-------+-------+-------+--------+-------+--------+-------+-------+--------+--------+
  42. | idsub | jan   | feb    | mar   | apr   | mei   | jun    | jul   | agu    | sep   | okt   | nov    | des    |
  43. +-------+-------+--------+-------+-------+-------+--------+-------+--------+-------+-------+--------+--------+
  44. |     1 | 59163 | 221354 |     0 | 98482 | 41395 | 112579 |     0 |      0 | 78641 |     0 |  23329 | 281390 |
  45. |     2 | 94502 |      0 |     0 | 89871 | 64326 |  19360 |     0 |      0 |     0 | 94337 | 104875 |      0 |
  46. |     3 | 46104 |      0 | 98808 |     0 | 59082 |   1948 | 53117 | 128447 |     0 | 69600 | 149811 |  43049 |
  47. +-------+-------+--------+-------+-------+-------+--------+-------+--------+-------+-------+--------+--------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement