Mysql:通过不同的值循环创建具有各种选择的过程

Mysql: create procedure with various selects through distinct value cycle

我有一个 table 这样的

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"

如何获得 table 中所有运算符的输出(不同的 STRCOUNTRY + STROPERATOR)并最终获得所有国家(其中 operator = #)的最终总和?

感谢您的帮助。

这是一个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