2016-04-08 90 views
1

是否有可能獲得相對於當前行的最後10行中的值的總和?如何獲取mysql中前10行的總和?

我已經創建了一個商店的數據庫,其中包含一個名爲purchase_details的表。該表的結構是:

+--------------------------------+---------------+-----+ 
| Field       | Type   | Key | 
+--------------------------------+---------------+-----+ 
| Trans_ID      | int(11)  | PRI | 
| Dealer_Name     | varchar(40) |  | 
| Todays_Purchase    | double(18,10) |  | 
| Total_Purchase_In_Last_10_Days | double(18,10) |  | 
+--------------------------------+---------------+-----+ 

的樣本數據:

+----------+-------------+-----------------+--------------------------------+ 
| Trans_ID | Dealer_Name | Todays_Purchase | Total_Purchase_In_Last_10_Days | 
+----------+-------------+-----------------+--------------------------------+ 
|  1 | Rahul  | 7769.1488285639 |       NULL | 
|  2 | Rahul  | 4158.5117578537 |       NULL | 
|  3 | Rahul  | 7200.1363099802 |       NULL | 
|  4 | Rahul  | 9338.8341269511 |       NULL | 
|  5 | Rahul  | 5897.7252866370 |       NULL | 
|  6 | Rahul  | 3266.6656585172 |       NULL | 
|  7 | Rahul  | 3188.0742696276 |       NULL | 
|  8 | Rahul  | 4270.5917314234 |       NULL | 
|  9 | Rahul  | 2604.3369713541 |       NULL | 
|  10 | Rahul  | 7908.6014441989 |       NULL | 
|  11 | Rahul  | 2693.4584823737 |       NULL | 
|  12 | Rahul  | 7945.7825034862 |       NULL | 
|  13 | Rahul  | 1904.1472157570 |       NULL | 
|  14 | Rajesh  | 7093.0478540344 |       NULL | 
|  15 | Rajesh  | 3219.3736989638 |       NULL | 
+----------+-------------+-----------------+--------------------------------+ 

我想獲得最後的10次交易完成購買的總和,但條件是應該有至少10次交易總結。

預期輸出:

+----------+-------------+-----------------+--------------------------------+ 
| Trans_ID | Dealer_Name | Todays_Purchase | Total_Purchase_In_Last_10_Days | 
+----------+-------------+-----------------+--------------------------------+ 
|  1 | Rahul  | 7769.1488285639 |     0.0000000000 | 
|  2 | Rahul  | 4158.5117578537 |     0.0000000000 | 
|  3 | Rahul  | 7200.1363099802 |     0.0000000000 | 
|  4 | Rahul  | 9338.8341269511 |     0.0000000000 | 
|  5 | Rahul  | 5897.7252866370 |     0.0000000000 | 
|  6 | Rahul  | 3266.6656585172 |     0.0000000000 | 
|  7 | Rahul  | 3188.0742696276 |     0.0000000000 | 
|  8 | Rahul  | 4270.5917314234 |     0.0000000000 | 
|  9 | Rahul  | 2604.3369713541 |     0.0000000000 | 
|  10 | Rahul  | 7908.6014441989 |    55602.6263900000 | 
|  11 | Rahul  | 2693.4584823737 |    50526.9360400000 | 
|  12 | Rahul  | 7945.7825034862 |    54314.2067800000 | 
|  13 | Rahul  | 1904.1472157570 |    49018.2176900000 | 
|  14 | Rajesh  | 7093.0478540344 |     0.0000000000 | 
|  15 | Rajesh  | 3219.3736989638 |     0.0000000000 | 
+----------+-------------+-----------------+--------------------------------+ 

對於這一點,我創建了一個MySQL的功能,將採取TRANS_ID和Dealer_Name作爲參數,並且將返回Todays_Purchase列的總和。

函數定義:

CREATE FUNCTION GET_TOTAL_PURCHASE(paramTransID INT, paramDealerName VARCHAR(40)) 
     RETURNS DOUBLE(18,10) 
    READS SQL DATA 
BEGIN 
    DECLARE totalPurchase DOUBLE(18,10); 
    SET totalPurchase = 0; 
    SELECT SUM(Todays_Purchase) 
     INTO totalPurchase 
     FROM purchase_details 
     WHERE Trans_ID > (paramTransID-10) 
      AND Trans_ID <= paramTransID 
      AND Dealer_Name = paramDealerName; 
    RETURN totalPurchase; 
END 

和SQL查詢來更新Total_Purchase_In_Last_10_Days列:

UPDATE purchase_details 
SET Total_Purchase_In_Last_10_Days = GET_TOTAL_PURCHASE(Trans_ID, Dealer_Name); 

上面的SQL正常工作,但它需要太多的時間來執行。表中有超過100萬條記錄,因此查詢需要5分鐘以上。鋤頭改善這個?

回答

0

導出的信息(如您要求的)在SELECTs中正確完成,而不是通過在表中使用冗餘代碼。

如果一個用戶提取他的信息,那麼計算即時總和將會相當便宜。你已經有FUNCTION這樣做了。

然而,你真的可以信任Trans_ID是連續的,沒有差距等?您的術語不一致:「_Last_10_Days」與「前10行」對比「Trans_」。如果有差距,「10天」可能會非常棘手。等等

+0

嗨瑞克,謝謝你的回覆。 是的,Trans_ID將始終是連續的。因此,在上面的例子中,當我計算Trans_ID = 12的行的總和時,我需要對前10行進行求和,即Trans_ID在3到12之間。 – Purab

+0

這應該是MariaDB 10.2的「窗口功能「。 –