MySql:如何用group_concat和Sum修复mysql查询?

时间:2017-02-17 14:43:27

标签: mysql

要求:

我正在尝试使用group_contact和sum来增强sql查询。但我不能成功。

SQL查询:

 SELECT 
    team.application_name AS 'Project',
    regression.automated_test_type AS 'SuiteType',
    SUM(regression.total_testcases) AS 'Total',
    SUM(regression.passed_testcases) AS 'Passed',
    SUM(regression.failed_testcases) AS 'Failed',
    regression.test_environment AS 'Environment'
FROM
    regression,
    team
WHERE
    execution_date >= CURDATE() - INTERVAL 7 DAY
        AND regression.automated_test_type <> 'Regression'
        AND regression.report_link NOT LIKE '%Automation_Q002envHc%'
        AND regression.report_link NOT LIKE '%LitVmEnvHealthChecks%'
        AND team.application_id = regression.regression_application_id
        AND team.application_name <> 'Not Mapped'
GROUP BY regression_application_id , regression.test_environment
ORDER BY team.application_name ASC , regression.automated_test_type ASC , regression.test_environment ASC;

当前查询输出:

----------------------------------------------------------------------
Project         SuiteType             Total Passed Failed Environment
----------------------------------------------------------------------
Application 1   Health Check            30    22    8     IQA
Application 2   Build Validation Test   33    21    12    IQA
Application 2   Build Validation Test   24    11    13    PROD
Application 3   Build Validation Test   399   321   78    IQA

预期输出

------------------------------------------------------
Project         SuiteType               Environment
------------------------------------------------------
Application 1   Health Check            IQA->30,22,8
Application 2   Build Validation Test   IQA->33,21,12|PROD->24,11,13  
Application 3   Build Validation Test   IQA->399,321,78

我根据评论和答案编辑了查询。仍然没有运气。

基于评论的编辑答案

     SELECT 
    T.Project,
    T.SuiteType,
    GROUP_CONCAT(CONCAT(T.Environment,
                '->',
                T.Passed,
                ',',
                T.Failed)
        ORDER BY T.Environment
        SEPARATOR '|') AS 'Environment'
FROM
    (SELECT 
        team.application_name AS 'Project',
            regression.automated_test_type AS 'SuiteType',
            SUM(regression.passed_testcases) AS 'Passed',
            SUM(regression.failed_testcases) AS 'Failed',
            regression.test_environment AS 'Environment'
    FROM
        regression, team
    WHERE
        execution_date >= CURDATE() - INTERVAL 7 DAY
            AND regression.automated_test_type <> 'Regression'
            AND regression.report_link NOT LIKE '%Automation_Q002envHc%'
            AND regression.report_link NOT LIKE '%LitVmEnvHealthChecks%'
            AND team.application_id = regression.regression_application_id
            AND team.application_name <> 'Not Mapped'
    GROUP BY regression_application_id , regression.test_environment
    ORDER BY team.application_name ASC , regression.automated_test_type ASC , regression.test_environment ASC) T
GROUP BY T.Project , T.SuiteType
ORDER BY T.Project , T.SuiteType

2 个答案:

答案 0 :(得分:1)

除非我遗漏了某些内容,否则看起来所需的输出列是已经返回的表达式和一些文字的串联

SELECT ...
     , CONCAT( expr1 , '->' , expr2 , ',' , expr3 , ',' , expr4 ) AS `Environment` 
  FROM ...

-

这样的事情:

SELECT ...
     , CONCAT( regression.test_environment
             , '->'
             , SUM(regression.total_testcases)
             , ','
             , SUM(regression.passed_testcases)
             , ','
             , SUM(regression.failed_testcases)
       ) AS `Environment` 
  FROM ...

注意,这些表达式中的任何一个都计算为NULL,来自CONCAT的返回值将为NULL。我们可能希望为NULL情况添加一些处理,例如使用IFNULL函数或CONCAT_WS代替CONCAT。

获得&#34;分组&#34;通过ProjectTestSuite,一个选项是采用上面的查询,并将其作为外部查询中的内联视图引用:

SELECT v.Project
     , v.SuiteType
     , GROUP_CONCAT(v.Environment ORDER BY v.Environment SEPARATOR '|') AS Environment
  FROM ( -- inline view query
         SELECT ...
              , CONCAT( regression.test_environment
                      , '->'
                      , SUM(regression.total_testcases)
                      , ','
                      , SUM(regression.passed_testcases)
                      , ','
                      , SUM(regression.failed_testcases)
                ) AS `Environment` 
           FROM ...

       ) v
 GROUP 
    BY v.Project
     , v.SuiteType  
 ORDER
    BY v.Project
     , v.SuiteType  

答案 1 :(得分:1)

<强> DEMO

Table1是您当前的查询输出。您可以用

替换它
FROM (SELECT <current query>) as T

SQL QUERY

SELECT `Project`, 
       `SuiteType`, 
       GROUP_CONCAT( CONCAT(`Environment`,'->', `Total`, ',', `Passed`, ',', `Failed`) 
                     ORDER BY `Environment`
                     SEPARATOR '|'
                   ) as `Environment`
FROM  Table1
GROUP BY `Project`, `SuiteType`

<强>输出

enter image description here