2015-04-01 81 views
0

我有這樣的表格回顧。此表用於我組織中的簡要數據教育。 我用mysql 5MySQL分組視圖

mysql> select * from rekap_teams.tblrekappendidikan_divisitahunan; 
+----+------------------------------------+-------+----+-----+-----+----+----+----+-----+----+----+-------+ 
| id | divisi        | year | SD | SMP | SMA | D1 | D2 | D3 | S1 | S2 | S3 | total | 
+----+------------------------------------+-------+----+-----+-----+----+----+----+-----+----+----+-------+ 
| 1 | CORPORATE       | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 25 | 4 | 0 | 29 | 
| 2 | CORPORATE SERVICES     | 2014 | 0 | 0 | 137 | 0 | 1 | 2 | 195 | 4 | 1 | 340 | 
| 3 | FINANCE       | 2014 | 0 | 0 | 0 | 0 | 1 | 2 | 54 | 2 | 0 | 59 | 
| 4 | SALES 1       | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 19 | 0 | 0 | 19 | 
| 5 | SALES 2       | 2014 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 12 | 
| 6 | PROMOTION       | 2014 | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 27 | 
| 7 | NON DIVISION - SALES & MARKETING | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 66 | 1 | 0 | 67 | 
| 8 | NON DIVISION - BOARD OF MANAGEMENT | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 |  9 | 
| 9 | PROGRAMMING      | 2014 | 0 | 0 | 0 | 0 | 1 | 0 | 28 | 3 | 0 | 32 | 
| 10 | OPERATIONAL BROADCAST    | 2014 | 0 | 0 | 4 | 0 | 9 | 9 | 376 | 3 | 0 | 401 | 
| 11 | NON DIVISION      | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  0 | 
| 12 | PRODUCTION 1      | 2014 | 0 | 0 | 1 | 0 | 1 | 2 | 124 | 1 | 0 | 129 | 
| 13 | PRODUCTION 2      | 2014 | 0 | 0 | 1 | 0 | 5 | 3 | 109 | 1 | 0 | 119 | 
| 14 | FILM, DRAMA & SPORT    | 2014 | 0 | 0 | 0 | 0 | 1 | 4 | 76 | 1 | 0 | 82 | 
| 15 | NEWS        | 2014 | 0 | 0 | 0 | 0 | 4 | 5 | 218 | 5 | 0 | 232 | 
| 16 | PRODUCTION FACILITIES    | 2014 | 0 | 0 | 2 | 0 | 4 | 13 | 597 | 5 | 0 | 621 | 
| 17 | PRODUCTION 4      | 2014 | 0 | 0 | 0 | 0 | 0 | 2 | 40 | 0 | 0 | 42 | 
| 18 | PCDC        | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 |  5 | 
| 19 | CORPORATE       | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 26 | 3 | 0 | 29 | 
| 20 | CORPORATE SERVICES     | 2015 | 0 | 0 | 130 | 0 | 1 | 1 | 194 | 2 | 1 | 329 | 
| 21 | FINANCE       | 2015 | 0 | 0 | 0 | 0 | 1 | 1 | 49 | 2 | 0 | 53 | 
| 22 | SALES 1       | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 18 | 0 | 0 | 18 | 
| 23 | SALES 2       | 2015 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 12 | 
| 24 | PROMOTION       | 2015 | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 27 | 
| 25 | NON DIVISION - SALES & MARKETING | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 64 | 1 | 0 | 65 | 
| 26 | NON DIVISION - BOARD OF MANAGEMENT | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 |  9 | 
| 27 | PROGRAMMING      | 2015 | 0 | 0 | 0 | 0 | 1 | 0 | 27 | 3 | 0 | 31 | 
| 28 | OPERATIONAL BROADCAST    | 2015 | 0 | 0 | 3 | 0 | 9 | 7 | 369 | 2 | 0 | 390 | 
| 29 | NON DIVISION      | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |  0 | 
| 30 | PRODUCTION 1      | 2015 | 0 | 0 | 1 | 0 | 1 | 2 | 117 | 1 | 0 | 122 | 
| 31 | PRODUCTION 2      | 2015 | 0 | 0 | 1 | 0 | 5 | 2 | 106 | 1 | 0 | 115 | 
| 32 | FILM, DRAMA & SPORT    | 2015 | 0 | 0 | 0 | 0 | 1 | 4 | 72 | 1 | 0 | 78 | 
| 33 | NEWS        | 2015 | 0 | 0 | 0 | 0 | 4 | 5 | 211 | 5 | 0 | 225 | 
| 34 | PRODUCTION FACILITIES    | 2015 | 0 | 0 | 2 | 0 | 2 | 6 | 583 | 5 | 0 | 598 | 
| 35 | PRODUCTION 4      | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 36 | 0 | 0 | 36 | 
| 36 | PCDC        | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 0 |  4 | 
+----+------------------------------------+-------+----+-----+-----+----+----+----+-----+----+----+-------+ 
36 rows in set (0.00 sec) 

我alwasy試圖解決我的分組查詢的問題。但我堆棧,查詢如何創建這樣的視圖?

+------------------------------------+----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+ 
| DIVISI        |    2014   |    2015   | 
+         +----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+ 
|         | SD | SMP | SMA | D1 | D2 | D3 | S1 | S2 | S3 | SD | SMP | SMA | D1 | D2 | D3 | S1 | S2 | S3 | 
+------------------------------------+----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+ 
| CORPORATE       | 0 | 0 | 0 | 0 | 0 | 0 | 25 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 25 | 4 | 0 | 
| CORPORATE SERVICES     | 0 | 0 | 137 | 0 | 1 | 2 | 195 | 4 | 1 | 0 | 0 | 137 | 0 | 1 | 2 | 195 | 4 | 1 | 
| FILM, DRAMA & SPORT    | 0 | 0 | 0 | 0 | 1 | 4 | 76 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 4 | 76 | 1 | 0 | 
| FINANCE       | 0 | 0 | 0 | 0 | 1 | 2 | 54 | 2 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 54 | 2 | 0 | 
| NEWS        | 0 | 0 | 0 | 0 | 4 | 5 | 218 | 5 | 0 | 0 | 0 | 0 | 0 | 4 | 5 | 218 | 5 | 0 | 
| NON DIVISION      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 
| NON DIVISION - BOARD OF MANAGEMENT | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 
| NON DIVISION - SALES & MARKETING | 0 | 0 | 0 | 0 | 0 | 0 | 66 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 66 | 1 | 0 | 
| OPERATIONAL BROADCAST    | 0 | 0 | 4 | 0 | 9 | 9 | 376 | 3 | 0 | 0 | 0 | 4 | 0 | 9 | 9 | 376 | 3 | 0 | 
| PCDC        | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 | 
| PRODUCTION 1      | 0 | 0 | 1 | 0 | 1 | 2 | 124 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 2 | 124 | 1 | 0 | 
| PRODUCTION 2      | 0 | 0 | 1 | 0 | 5 | 3 | 109 | 1 | 0 | 0 | 0 | 1 | 0 | 5 | 3 | 109 | 1 | 0 | 
| PRODUCTION 4      | 0 | 0 | 0 | 0 | 0 | 2 | 40 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 40 | 0 | 0 | 
| PRODUCTION FACILITIES    | 0 | 0 | 2 | 0 | 4 | 13 | 597 | 5 | 0 | 0 | 0 | 2 | 0 | 4 | 13 | 597 | 5 | 0 | 
| PROGRAMMING      | 0 | 0 | 0 | 0 | 1 | 0 | 28 | 3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 28 | 3 | 0 | 
| PROMOTION       | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 
| SALES 1       | 0 | 0 | 0 | 0 | 0 | 0 | 19 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 19 | 0 | 0 | 
| SALES 2       | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 
+------------------------------------+----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+ 

回答

-2

如果你知道你有這些年來,下面的查詢將讓你在你想要的表單數據。如果您希望按照某個順序或者在您的示例中省略某些列,請更改字段列表。

select * from rekap_teams.tblrekappendidikan_divisitahunan as t2014 join 
    rekap_teams.tblrekappendidikan_divisitahunan as t2015 on(DIVISI) 
    where t2014.year = 2014 and t2015.year=2015 

將*替換爲列名的順序。

+0

他想分組 – 2015-04-01 06:41:17

+0

但他的樣本數據沒有任何分組。他們每年只有一個入口。 – Rasika 2015-04-01 10:20:03