MySQL-尝试将`GROUP_CONCAT`与`COUNT()`结合使用

时间:2018-06-29 06:03:18

标签: mysql group-concat

SELECT A.id  ,A.Prospect_Id, GROUP_CONCAT(DISTINCT A.MYVIEW SEPARATOR ' , ') AS myview
FROM
   (SELECT id, Prospect_Id, CONCAT(Current_Status, '(', count(Current_Status), ')') AS MYVIEW     
    FROM prospectdata1
    GROUP BY Prospect_Id) A
GROUP BY  A.prospect_id
HAVING myview REGEXP 'PHONE_FOLLOW_UP.*DUPLICATE'

输出: enter image description here

预期输出: enter image description here

1 个答案:

答案 0 :(得分:0)

您需要同时按Prospect_Id和Current_Status分组:

SELECT MIN(MinId) AS id
     , Prospect_Id
     , GROUP_CONCAT(CONCAT(Current_Status, '(', SUM(Cnt), ')') SEPARATOR ' , ') AS myview
FROM (
  SELECT Prospect_Id
       , Current_Status
       , COUNT(*) Cnt
       , MIN(id) MinId
   FROM prospectdata1
  WHERE Current_Status REGEXP 'PHONE_FOLLOW_UP.*DUPLICATE'
  GROUP BY Prospect_Id
      , Current_Status
) A
GROUP BY prospect_id