2014-08-30 44 views
0

我有具有以下結構的表格:限制記錄平均結果之前整體的MySQL

doc_date date, doc_type varchar, code int primary key, qty int, price double 

代碼從1範圍 - 10; doc_type包括LP,FP,CAS,CRS;

我想查詢該表爲每個代碼其中DOC_TYPE要麼是「LP」或「FP」,然後得到N個數量的記錄(doc_date的降序排列)最新平均價格價格

除了在平均價格之前限制每個代碼的記錄外,我已經能夠做到我所需要的一切。

我的查詢是:

SELECT a.sfcode, a.productname, COALESCE((b.QTY - COALESCE(c.QTY,0)),0) AS currentstock, a.lastprice AS lprice, COALESCE(CAST(d.price AS decimal(16,3)),0) AS sprice, COALESCE(CAST(e.price AS decimal(16,3)),0) AS cprice FROM 

(SELECT sfcode, productname, lastprice FROM products) AS a LEFT JOIN 

(SELECT sfcode, SUM(COALESCE(QTY, 0)) AS QTY FROM transactions WHERE doctype IN ('FP','LP','CSR') GROUP BY sfcode) AS b ON a.sfcode = b.sfcode LEFT JOIN 

(SELECT sfcode, SUM(QTY) AS QTY FROM transactions WHERE doctype IN ('FPR','LPR','CAS','CRS') GROUP BY sfcode) AS c ON a.sfcode = c.sfcode LEFT JOIN 

(SELECT sfcode, AVG(unitprice) AS price FROM transactions WHERE doctype IN ('CAS', 'CRS') GROUP BY sfcode) AS d ON a.sfcode = d.sfcode LEFT JOIN 

(SELECT sfcode, AVG(unitprice) AS price FROM transactions WHERE doctype IN ('FP', 'LP') GROUP BY sfcode) AS e ON a.sfcode = e.sfcode ORDER BY sfcode; 

最後2行查詢的是我需要的價格平均

...我已經尋找此之前限制遞減日期順序記錄和找到http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/我能夠獲得有限的行,但我不知道如何添加doc_type子句並獲取基於desc日期的結果。

鏈接sqlfiddle http://sqlfiddle.com/#!2/4778b/1

我想要的結果看起來像這樣..當限制設置爲3:

| sfcode | stock | lprice | sprice | cprice | 
| 1 | 40 | 15.000 | 0.000 | 9.667 | 
| 2 | 80 | 25.000 | 0.000 | 19.667 | 
| 3 | 120 | 35.000 | 0.000 | 29.66 | 
| 4 | 160 | 45.000 | 0.000 | 39.667 | 
| 5 | 200 | 55.000 | 0.000 | 49.667 | 
| 6 | 0 | 65.000 | 0.000 | 0.000 | 
| 7 | 0 | 75.000 | 0.000 | 0.000 | 
| 8 | 0 | 85.000 | 0.000 | 0.000 | 
| 9 | 0 | 95.000 | 0.000 | 0.000 | 
| 10 | 0 |105.000 | 0.000 | 0.000 | 
+0

可以請您發佈一個sqlfiddle模式和數據供我們測試。沒有太多,我們可以這樣做,因爲 – 2014-08-30 22:37:35

+0

你想要什麼限制?像一定數量的記錄?那會是多少? – 2014-08-31 00:19:46

+0

你的預期結果是什麼? – 2014-08-31 00:21:21

回答

0

因此,基於該link你提供,你可以改變你查詢

set @row_limit = 3; 
set @row_num = 0; 
set @code = -1; 

SELECT a.sfcode, COALESCE((b.QTY - COALESCE(c.QTY,0)),0) AS currentstock, 
a.lastprice AS lprice, COALESCE(CAST(d.price AS decimal(16,3)),0) AS sprice, 
COALESCE(CAST(e.price AS decimal(16,3)),0) AS cprice FROM 
(SELECT sfcode, lastprice FROM products) AS a LEFT JOIN 
(SELECT sfcode, SUM(COALESCE(QTY, 0)) AS QTY 
    FROM transactions 
    WHERE doctype IN ('FP','LP','CSR') 
    GROUP BY sfcode) AS b ON a.sfcode = b.sfcode LEFT JOIN 
(SELECT sfcode, SUM(QTY) AS QTY 
    FROM transactions 
    WHERE doctype IN ('FPR','LPR','CAS','CRS') 
GROUP BY sfcode) AS c ON a.sfcode = c.sfcode LEFT JOIN 
(SELECT sfcode, AVG(unitprice) AS price 
FROM (SELECT sfcode, unitprice, 
    @row_num := if(@code = sfcode, @row_num + 1, 0) AS rowNumber, 
    @code := sfcode AS code 
    FROM transactions 
    WHERE doctype IN ('CAS', 'CRS') 
    ORDER BY sfcode, docdate DESC) AS f 
WHERE f.rowNumber < @row_limit GROUP BY sfcode) 
AS d ON a.sfcode = d.sfcode 
LEFT JOIN (SELECT sfcode, AVG(unitprice) AS price 
FROM **(SELECT sfcode, unitprice, 
    @row_num := if(@code = sfcode, @row_num + 1, 0) as rowNumber, 
    @code := sfcode AS code 
    FROM transactions 
    WHERE doctype IN ('FP', 'LP') 
    ORDER BY sfcode, docdate DESC) AS g** 
WHERE g.rowNumber < @row_limit GROUP BY sfcode) 
AS e ON a.sfcode = e.sfcode ORDER BY sfcode; 

其中@row_limit設置爲你希望收集最新的交易數量。

+0

工程像魅力!非常感謝。 – HMH 2014-08-31 02:40:18