簡體   English   中英

查詢以顯示同一表格中多列和多行的分組信息

[英]Query to display grouped info from same table in more than one column and row

我正在努力進行單個mysql查詢,以從一個表中獲取數據,該表包含有關包含測試用例的測試請求結果的信息。 每個測試用例可以有4個不同的結果。 結構如下(帶有示例數據):

testrequestsresultsID | testrequestsID | testcaseresultsID
1                     | 1              | 4
2                     | 1              | 2
3                     | 1              | 1
4                     | 1              | 1
5                     | 2              | 4
6                     | 2              | 4
7                     | 2              | 2
8                     | 2              | 2 

我的基本目標是在4個不同的列中顯示有關測試請求進度的信息。

  1. 第一欄應顯示測試請求的總數,
  2. 第二列應顯示已完成的測試請求(testcaseresultsID不同於4)
  3. 第三列應顯示已通過的測試用例的數量(testcaseresultsID等於1)
  4. 第四列應顯示失敗的測試用例數(testcaseresultsID等於2)

我設法通過以下查詢按一個測試請求ID獲取該信息:

SELECT 
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1) `total`,
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1 AND `testcaseresultsID`<>4) `finished`,
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1 AND `testcaseresultsID`=1) `pass`,
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1 AND `testcaseresultsID`=2) `fail`

這僅顯示一個請求的信息。

我的問題是如何顯示許多行的擴展信息,並在前列顯示testrequestsID? 考慮示例數據,它應如下所示顯示:

testrequestsID | total | finished | pass | fail
1              | 4     | 3        | 2    | 1
2              | 4     | 2        | 0    | 2

預先感謝您的幫助。

SELECT testrequestsID,
SUM IF(`testrequestsID`=1, 1,0) as total,
SUM IF(`testrequestsID`=1 AND `testcaseresultsID`<>4, 1, 0) AS finished,
SUM IF(`testrequestsID`=1 AND `testcaseresultsID`=1, 1, 0) AS pass,
SUM IF(`testrequestsID`=1 AND `testcaseresultsID`=2, 1, 0) AS fail
FROM
GROUP BY testrequestsID
SELECT testrequestsid
     , COUNT(*) total
     , SUM(testcaseresultsid <> 4) finished
     , SUM(testcaseresultsid = 1) pass
     , SUM(testcaseresultsid = 2) fail 
  FROM my_table 
 GROUP 
    BY testrequestsid;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM