我有这样的桌子

TIME  |STRCOUNTRY |STROPERATOR | NINCOMINGCALLS
300718|RUSSIA     |MTS         | 2
300718|RUSSIA     |Megafon     | 3
300718|UK         |Vodafone    | 1
300718|UK         |UKTele      | 3




最后是我要显示的:要求运营商,按国家/地区计算所有运营商的总和

RUSSIA  |Megafon |  3
RUSSIA  |MTS     |  2
RUSSIA  |#       |  5
UK      |Vodafone|  1
UK      |UKTele  |  3
UK      |#       |  4
#       | #      | 9


可以通过这样的查询来接收

select * from TM_COMMON_STAT where strCountry = "RUSSIA" and TIME = "300718"

UNION


select STRCOUNTRY, '#' as STROPERATOR, SUM(NINCOMINGCALLS) as 'NINCOMINGCALLS' from TM_COMMON_STAT where strCountry = "RUSSIA" and TIME = "300718"


如何获得表中所有运算符的输出(不同的STRCOUNTRY + STROPERATOR),最后得到所有国家的最终总和(运算符=#)?

谢谢您的帮助。

最佳答案

这是job for GROUP BY...WITH ROLLUP

尝试这个

SELECT STRCOUNTRY, STROPERATOR, SUM(NINCOMINGCALLS) NINCOMINGCALLS
  FROM TM_COMMON_STAT
 WHERE <<<whatever filters you require >>>
 GROUP BY STRCOUNTRY, STROPERATOR WITH ROLLUP

10-08 09:35