2013-12-21 131 views
0

我是相對較新的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 = /總其中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  | 
+---------+-----------------+-------+-------+------------+ 

回答

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; 
+0

您好,戈登,這個代碼就像一個魅力。非常感謝幫助。我有兩個問題,但如果你可以幫忙的話,它會是你的。)我無法理解應用左外連接的邏輯,我可以請你詳細說明它2.)在**之前還有一個額外的** ** **在子查詢中我試圖修改它,但是由於某種原因,我無法在答案上這麼做..如何解決它 - 再次感謝噸,以節省我的皮膚:) –

+0

「total」之前的單引號是一個錯字。 「左外連接」並不是真的有必要。它只強調在第一個表中的所有行都保留在輸出中。 –

+0

感謝您的輸入先生,我現在意識到,我想更新現有的表** ** Calc **列輸出,我嘗試了切斷方法做到這一點,但我最終與_Error代碼:1064 您在您的錯誤SQL語法; _我可以尋求您的指導嗎? –