我試圖做表,我下面有一個簡單的數學計算權重,如何計算在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 |
+-----------+---------+----------+-------+-------+---------+
你有總城市數爲表中有一個單獨的行?那將是一個奇怪的桌子設計。 –
是否有一行'Cty_lst ='ALL''的值或計算的值? – AgRizzo
什麼是srvy_cty,你不在select語句中引用它? – AgRizzo