2016-08-30 45 views
2

我目前正在實習中,我必須創建一個缺失的視圖,這會阻止網站完美工作。使用計數(*)與多個GROUP BY的SQL SELECT請求

該視圖包含多個信息,而且還與每對夫婦的狀態/進程的行數,如下:

+----------------+---------+----------+-----------+-----------+--------+ 
| status   | process | project | idprocess | idproject | number | 
+----------------+---------+----------+-----------+-----------+--------+ 
| Not applicable | AP  | tms-data |  17 |   2 | 432 | 
| Not applicable | AP+51 | tms-data |  18 |   2 | 432 | 
| Not applicable | AR  | tms-data |  19 |   2 | 432 | 
| Realized  | AP  | tms-data |  17 |   2 | 432 | 
| Realized  | AP+51 | tms-data |  18 |   2 | 432 | 
| Realized  | AR  | tms-data |  19 |   2 | 432 | 
| Safety   | AP  | tms-data |  17 |   2 | 432 | 
| Safety   | AP+51 | tms-data |  18 |   2 | 432 | 
| Safety   | AR  | tms-data |  19 |   2 | 432 | 
| Ticket   | AP  | tms-data |  17 |   2 | 432 | 
| Ticket   | AP+51 | tms-data |  18 |   2 | 432 | 
| Ticket   | AR  | tms-data |  19 |   2 | 432 | 
| To be designed | AP  | tms-data |  17 |   2 | 432 | 
| To be designed | AP+51 | tms-data |  18 |   2 | 432 | 
| To be designed | AR  | tms-data |  19 |   2 | 432 | 
| Validated  | AP  | tms-data |  17 |   2 | 432 | 
| Validated  | AP+51 | tms-data |  18 |   2 | 432 | 
| Validated  | AR  | tms-data |  19 |   2 | 432 | 
+----------------+---------+----------+-----------+-----------+--------+ 
18 rows in set (0.03 sec) 

我使用MySQL。我不明白爲什麼這麼重要,有什麼想法?

這裏是我一直使用至今的腳本:

SELECT alstom_status.name AS status, 
     alstom_process.name AS process, 
     alstom_project.name AS project, 
     alstom_process.idprocess AS idprocess, 
     alstom_project.idproject AS idproject, 
     count(*) AS number 
FROM alstom_rule INNER JOIN alstom_status ON idstatus 
        INNER JOIN alstom_project ON idproject 
        INNER JOIN alstom_process ON idprocess 
GROUP BY alstom_status.name, alstom_process.name 
ORDER BY process; 
+1

你需要5個組by by不是2我相信 – Drew

+1

你的GROUP BY子句看起來是正確的,但是你不應該選擇列,除非它們也出現在GROUP BY子句中,或者在集合函數裏面像' COUNT'。 –

+0

@Drew你可能是對的。 –

回答

-1
This works always for me ! 

SELECT alstom_status.name AS status, 
      alstom_process.name AS process, 
      alstom_project.name AS project, 
      alstom_process.idprocess AS idprocess, 
      alstom_project.idproject AS idproject, 
      count(*) AS number 
    FROM alstom_rule INNER JOIN alstom_status ON idstatus 
        INNER JOIN alstom_project ON idproject 
        INNER JOIN alstom_process ON idprocess 
    GROUP BY status, process,project,idprocess, idproject; 

您可以使用(按alstom_status.name,alstom_process.name,alstom_project.name,alstom_process.idprocess,alstom_project。 idproject)