如何在mysql中计算权重

时间:2014-01-08 13:21:19

标签: mysql

我正在尝试在下面的表格上进行简单的数学计算,

  +-----------+---------+----------+-------+-------+
  | Cty_lst   | Cty_Rgn | Criteria | Total | Sntmt |
  +-----------+---------+----------+-------+-------+
  | ALL       | ALL     | ALL      |  3155 | ALL   |
  | Bangalore | ALL     | ALL      |   628 | ALL   |
  | Chennai   | ALL     | ALL      |   337 | ALL   |
  | Delhi     | ALL     | ALL      |   263 | ALL   |
  | Gurgaon   | ALL     | ALL      |   282 | ALL   |
  | Hyderabad | ALL     | ALL      |   406 | ALL   |
  | Mumbai    | ALL     | ALL      |   482 | ALL   |
  | Noida     | ALL     | ALL      |   235 | ALL   |
  | Pune      | ALL     | ALL      |   522 | ALL   |
  +-----------+---------+----------+-------+-------+

我正在尝试计算构成所有人的每个城市的权重,如下面的结果表,wt_calc是通过除以城市总数/所有总数来计算的 例如城市班加罗尔wt由628/3155 = 19.90计算。我尝试了以下代码的许多变体

SELECT Cty_lst , cty_rgn,Criteria, total,Sntmt,
       (100*SUM(CASE WHEN Sntmt = 'all'  AND Cty_lst != 'all' THEN total END) 
       / 
       SUM(CASE WHEN Sntmt = 'all' AND Cty_lst = 'all'  THEN total END) 
       ) AS wt_calc
FROM citytable
GROUP BY Srvy_cty

  +-----------+---------+----------+-------+-------+---------+
  | Cty_lst   | Cty_Rgn | Criteria | Total | Sntmt | Wt_Calc |
  +-----------+---------+----------+-------+-------+---------+
  | ALL       | ALL     | ALL      |  3155 | ALL   | 100     |
  | Bangalore | ALL     | ALL      |   628 | ALL   | 19.90   |
  | Chennai   | ALL     | ALL      |   337 | ALL   | 10.68   |
  | Delhi     | ALL     | ALL      |   263 | ALL   | 8.34    |
  | Gurgaon   | ALL     | ALL      |   282 | ALL   | 8.94    |
  | Hyderabad | ALL     | ALL      |   406 | ALL   | 12.87   |
  | Mumbai    | ALL     | ALL      |   482 | ALL   | 15.28   |
  | Noida     | ALL     | ALL      |   235 | ALL   | 7.45    |
  | Pune      | ALL     | ALL      |   522 | ALL   | 16.55   |
  +-----------+---------+----------+-------+-------+---------+

5 个答案:

答案 0 :(得分:0)

将所有值丢弃,然后使用:

SELECT Cty_lst , cty_rgn,Criteria, total,Sntmt, 
    Total/(SELECT SUM(Total) FROM citytable) AS wt_calc
FROM citytable

http://sqlfiddle.com/#!2/27040f/7

答案 1 :(得分:0)

CREATE TABLE Table1
    (`Cty_lst` varchar(9), `Cty_Rgn` varchar(3), `Criteria` varchar(3), `Total` int, `Sntmt` varchar(3))
;

INSERT INTO Table1
    (`Cty_lst`, `Cty_Rgn`, `Criteria`, `Total`, `Sntmt`)
VALUES
    ('ALL', 'ALL', 'ALL', 3155, 'ALL'),
    ('Bangalore', 'ALL', 'ALL', 628, 'ALL'),
    ('Chennai', 'ALL', 'ALL', 337, 'ALL'),
    ('Delhi', 'ALL', 'ALL', 263, 'ALL'),
    ('Gurgaon', 'ALL', 'ALL', 282, 'ALL'),
    ('Hyderabad', 'ALL', 'ALL', 406, 'ALL'),
    ('Mumbai', 'ALL', 'ALL', 482, 'ALL'),
    ('Noida', 'ALL', 'ALL', 235, 'ALL'),
    ('Pune', 'ALL', 'ALL', 522, 'ALL')
;

select
t.*
, Total / @total * 100
from
Table1 t
, (select @total := (select Total from Table1 where Cty_lst = 'ALL' limit 1)) vars

|   CTY_LST | CTY_RGN | CRITERIA | TOTAL | SNTMT | TOTAL / @TOTAL * 100 |
|-----------|---------|----------|-------|-------|----------------------|
|       ALL |     ALL |      ALL |  3155 |   ALL |                  100 |
| Bangalore |     ALL |      ALL |   628 |   ALL |              19.9049 |
|   Chennai |     ALL |      ALL |   337 |   ALL |              10.6815 |
|     Delhi |     ALL |      ALL |   263 |   ALL |                8.336 |
|   Gurgaon |     ALL |      ALL |   282 |   ALL |               8.9382 |
| Hyderabad |     ALL |      ALL |   406 |   ALL |              12.8685 |
|    Mumbai |     ALL |      ALL |   482 |   ALL |              15.2773 |
|     Noida |     ALL |      ALL |   235 |   ALL |               7.4485 |
|      Pune |     ALL |      ALL |   522 |   ALL |              16.5452 |

答案 2 :(得分:0)

如果我理解你的话,应该这样做:

SELECT Cty_lst , cty_rgn,Criteria, total,Sntmt,
       (100* total) /
       (SELECT total FROM citytable WHERE  Sntmt = 'all' AND Cty_lst = 'all')
       ) AS wt_calc
FROM citytable

答案 3 :(得分:0)

尝试

SELECT 
      a.*,
     round((a.Total/SUM(b.Total))*100,2) as weight
FROM 
      citytable as a,
      citytable as b
WHERE
      b.CTY_LST != 'ALL'
GROUP BY
      a.CTY_LST;

演示http://sqlfiddle.com/#!2/cbc23/17

答案 4 :(得分:-1)

我不会把这些城市的总数连在一起。如果你想保留它,我会做类似以下的事情:

SELECT Cty_lst , cty_rgn,Criteria, total,Sntmt,      
   (CASE when Sntmt = 'all'  AND Cty_lst != 'all' THEN total ELSE total / sum(total)
END) AS wt_calc
FROM citytable
GROUP BY Srvy_cty

但更好的方法是删除总行并执行此操作:

SELECT Cty_lst , cty_rgn,Criteria, total,Sntmt,      
   total / sum(total) as wt_calc
FROM citytable
GROUP BY Srvy_cty