基于mysql中的行内容计算数学计算

时间:2013-12-21 16:48:40

标签: mysql

我对Mysql相对较新,并试图找出一种在行之间进行简单数学计算的方法,下面是我正在使用的视图示例,我想计算一些基本函数


+-----------------+-------+-------+------------+
|   Buy_cty_rgn   | total | Sntmt | Sntmt_Calc |
+-----------------+-------+-------+------------+
| Central Chennai |    24 | D     |            |
| Central Chennai |    11 | I     |            |
| Central Chennai |     2 | S     |            |
| Central Chennai |    37 | All   |            |
| North Chennai   |    11 | D     |            |
| North Chennai   |    17 | I     |            |
| North Chennai   |     2 | S     |            |
| North Chennai   |    30 | All   |            |
| South Chennai   |   113 | D     |            |
| South Chennai   |   108 | I     |            |
| South Chennai   |    28 | S     |            |
| South Chennai   |   249 | All   |            |
| West Chennai    |     7 | D     |            |
| West Chennai    |     8 | I     |            |
| West Chennai    |     6 | S     |            |
| West Chennai    |    21 | All   |            |
| All             |   337 | All   |            |
+-----------------+-------+-------+------------+

Snt_Calc =((总计Sntmt = D /总计,其中sntmt = 全部)* 100) - ((总计Sntmt = I / total其中sntmt = 全部)* 100)

期望输出


+---------+-----------------+-------+-------+------------+
| Dmc_Cty |   Buy_cty_rgn   | total | Sntmt | Calc_Sntmt |
+---------+-----------------+-------+-------+------------+
| Chennai | Central Chennai |    24 | D     |            |
| Chennai | Central Chennai |    11 | I     |            |
| Chennai | Central Chennai |     2 | S     |            |
| Chennai | Central Chennai |    37 | All   | 35.14      |
| Chennai | North Chennai   |    11 | D     |            |
| Chennai | North Chennai   |    17 | I     |            |
| Chennai | North Chennai   |     2 | S     |            |
| Chennai | North Chennai   |    30 | All   | -20.00     |
| Chennai | South Chennai   |   113 | D     |            |
| Chennai | South Chennai   |   108 | I     |            |
| Chennai | South Chennai   |    28 | S     |            |
| Chennai | South Chennai   |   249 | All   | 2.01       |
| Chennai | West Chennai    |     7 | D     |            |
| Chennai | West Chennai    |     8 | I     |            |
| Chennai | West Chennai    |     6 | S     |            |
| Chennai | West Chennai    |    21 | All   | -4.76      |
+---------+-----------------+-------+-------+------------+

1 个答案:

答案 0 :(得分:0)

将计算作为子查询进行,然后将结果重新加入:

计算值的查询:

select Dmc_Cty, Buy_cty_rgn,
       (100*sum(case when Sntmt = 'D' then total end) / sum(case when Sntmt = 'All' then total end) -
        100*sum(case when Sntmt = 'I' then total end) / sum(case when Sntmt = 'All' then total end)
       ) as Calc_Sntmt
from table t
group by Dmc_Cty, Buy_cty_rgn;

将结果加入的查询:

select t.*, (case when t.Sntmt = 'All' then tsum. Calc_Sntmt end) as Calc_Sntmt
from table t left outer join
     (select Dmc_Cty, Buy_cty_rgn,
             (100*sum(case when Sntmt = 'D' then total end) / sum(case when Sntmt = 'All' then total end) -
              100*sum(case when Sntmt = 'I' then total end) / sum(case when Sntmt = 'All' then total end)
             ) as Calc_Sntmt
      from table t
      group by Dmc_Cty, Buy_cty_rgn
     ) tsum
     on tsum.Dmc_Cty = t.Dmc_Cty and
        tsum.Buy_cty_rgn = t.Buy_cty_rgn;