2

我将计算总区域的记录,其中状态会有所不同,例如'PENDING','SENT INFO','PTP',分组查询和多个选择查询,但结果没有得到所以请帮助我。

我的查询是:

SELECT AREA, 
(SELECT COUNT( * ) FROM prospectmaster WHERE ZONE = 'AHMEDABAD' && STATUS = 'PENDING' GROUP BY AREA) AS PENDING, 
(SELECT COUNT( * ) FROM prospectmaster WHERE ZONE = 'AHMEDABAD' && STATUS = 'SENT INFO.' GROUP BY AREA) AS CNT 
FROM prospectmaster 
WHERE ZONE = 'AHMEDABAD' GROUP BY AREA  

我想要这种类型的结果。

AREA       PENDING    INFO SENT     PTP 
AHMEDABAD   1          2             1    
GANDHINAGAR 1          5             4
KHEDA       3         10             9

我认为查询中有问题,但我不明白。所以,请帮助我。

4

3 回答 3

5
SELECT AREA, 
       SUM(CASE WHEN STATUS = 'PENDING' THEN 1 ELSE 0 END) AS PENDING, 
       SUM(CASE WHEN STATUS = 'SENT INFO.' THEN 1 ELSE 0 END) AS cnt, 
       SUM(CASE WHEN STATUS = 'PTP' THEN 1 ELSE 0 END) AS PTP
  FROM prospectmaster 
 WHERE ZONE = 'AHMEDABAD' 
 GROUP BY AREA;

如果STATUS有“已发送信息”。记录在数据库中.

于 2013-07-08T06:50:47.627 回答
1
  SELECT AREA, 
         SUM(if(STATUS='PENDING',1,0)) AS PENDING, 
         SUM(if(STATUS='SENT INFO',1,0)) AS "INFO SENT",
         SUM(if(STATUS='PTP',1,0)) AS PTP 
    FROM prospectmaster 
   WHERE ZONE = 'AHMEDABAD' 
GROUP BY AREA;
于 2013-07-08T06:57:16.017 回答
0

因为这是mysql,你可以简单地这样做:

SELECT
  AREA, 
  SUM(STATUS = 'PENDING') AS PENDING, 
  SUM(STATUS = 'SENT INFO.') AS SENT_INFO,
  SUM(STATUS = 'PTP') AS PTP
FROM prospectmaster 
WHERE ZONE = 'AHMEDABAD' 
GROUP BY AREA

这是有效的,因为在 mysql 中,true 为 1,false 为 0,因此对条件求和计算其为 true 的次数

于 2013-07-08T07:37:08.950 回答