2014-01-08 75 views
2

我試圖做表,我下面有一個簡單的數學計算權重,如何計算在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 | 
    +-----------+---------+----------+-------+-------+ 

我試圖計算重量每個consititutes所有城市,類似的結果下表中,wt_calc通過城市總數/所有總數 例如城市班加羅爾重量計算爲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 | 
    +-----------+---------+----------+-------+-------+---------+ 
+0

你有總城市數爲表中有一個單獨的行?那將是一個奇怪的桌子設計。 –

+0

是否有一行'Cty_lst ='ALL''的值或計算的值? – AgRizzo

+0

什麼是srvy_cty,你不在select語句中引用它? – AgRizzo

回答

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 | 
-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 
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

+0

嘗試了建議的代碼,但沒有得到預期的結果,因爲所有值都返回爲1 –

+0

請參閱小提琴!有用。 – niyou

+0

我檢查了小提琴,這是[結果](http://sqlfiddle.com/#!2/27040f/6)我得到 –

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 
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