使用TOTAL进行透视查询

时间:2017-01-09 15:22:31

标签: mysql

再次需要帮助。

我有这个查询来转动一个表,如下所示:

    SELECT *
FROM (
SELECT
CASE WHEN ISNULL(country) THEN 'APAC' ELSE country END AS COUNTRY,
IFNULL(SUM(CASE WHEN MONTH(calldate)=1 THEN duration END),'---') AS 'Jan16',
 IFNULL(SUM(CASE WHEN MONTH(calldate)=2 THEN duration END),'---') AS 'Feb16',
  IFNULL(SUM(CASE WHEN MONTH(calldate)=3 THEN duration END),'---') AS 'Mar16',
   IFNULL(SUM(CASE WHEN MONTH(calldate)=4 THEN duration END),'---') AS 'Apr16',
    IFNULL(SUM(CASE WHEN MONTH(calldate)=5 THEN duration END),'---') AS 'May16',
     IFNULL(SUM(CASE WHEN MONTH(calldate)=6 THEN duration END),'---') AS 'Jun16',
      IFNULL(SUM(CASE WHEN MONTH(calldate)=7 THEN duration END),'---') AS 'Jul16',
       IFNULL(SUM(CASE WHEN MONTH(calldate)=8 THEN duration END),'---') AS 'Aug16',
        IFNULL(SUM(CASE WHEN MONTH(calldate)=9 THEN duration END),'---') AS 'Sept16',
         IFNULL(SUM(CASE WHEN MONTH(calldate)=10 THEN duration END),'---') AS 'Oct16',
          IFNULL(SUM(CASE WHEN MONTH(calldate)=11 THEN duration END),'---') AS 'Nov16',
           IFNULL(SUM(CASE WHEN MONTH(calldate)=12 THEN duration END),'---') AS 'Dec16',
IFNULL((SUM(CASE WHEN MONTH(calldate)=11 THEN duration END)-
SUM(CASE WHEN MONTH(calldate)=12 THEN duration END)),'---') AS 'BW'
FROM
(SELECT cdr_users.`parts`,cdr.*, cdr_users.`country` FROM cdr LEFT JOIN cdr_users ON cdr.dst = cdr_users.`parts`
WHERE (CHAR_LENGTH(dst)=6 AND LEFT(dst, 3) IN ('123','124','125','126'))
AND (calldate >=  '2016-01-01 00:00:00' AND calldate <= '2016-12-31 23:59:59')) AS cdr 
GROUP BY country WITH ROLLUP
) AS cdr1 

现在结果如下: enter image description here 问题:

正如你所看到的,底部行显示TOTALS就好了,但是,这个名字仍然是APAC。 这是因为第一次CASE声明。如何将其更改为TOTAL而不是APAC,将第1行更改为APAC?

谢谢,

0 个答案:

没有答案