-1
StockCode TimeStamp UnitPrice TrnDate
---------------------------------------------------------------
360120 0x000000000DBE9EED 16.8000 2015-02-13 0:00:00.000
360120 0x000000000DBEE175 16.8000 2015-02-17 00:00:00.000
360120 0x000000000DC177AC 16.8000 2015-02-24 00:00:00.000
360120 0x000000000DC1EEFB 16.0000 2015-02-25 00:00:00.000
360120 0x000000000DC1E9BE 16.8000 2015-02-25 00:00:00.000
360120 0x000000000DC261CE 16.0000 2015-02-27 00:00:00.000
360120 0x000000000DC2628D 16.8000 2015-02-27 00:00:00.000 **
360120 0x000000000DFC46EA 16.8000 2015-03-02 00:00:00.000
查詢需要返回2015年2月27日,其中的價格去的形式向16.000 16.8000, 這是在價格歷史表這個項目的最後價格的更改日期。 我寫它的方式是,查詢只在價格沒有變回表格中存在的股票代碼價格時才起作用。獲取最後更改日期在SQL
SELECT
MAX(MinTrnDate) as LastTrnDate
FROM
(SELECT
a.UnitPrice, MIN(TrnDate) as MinTrnDate
FROM
(SELECT
ph.StockCode,
CONVERT(DECIMAL(18,4), (ph.InvoiceValue/ ph.InvoiceQty)) as UnitPrice,
ph.TrnDate
FROM
ArSalesMove ph
JOIN
ArCustomer c WITH(NOLOCK) on ph.Customer = c.Customer
WHERE
LTRIM(RTRIM(ph.StockCode)) = '360120'
AND (ph.InvoiceValue/ ph.InvoiceQty) > 0
AND c.PriceCode = 'A') AS a
WHERE
a.UnitPrice > 0
GROUP BY a.UnitPrice) AS b
HAVING
MIN(CONVERT(DECIMAL(18, 2), UnitPrice)) <> MAX(CONVERT(DECIMAL(18, 2), UnitPrice)) --Used to exclude stockcodes with no change...
我懷疑這是有可能使用窗口功能可以輕鬆解決,但他們在所有RDBMS上不受支持。你使用的是什麼RDBMS? – JNevill 2015-03-02 20:26:28
這是SQL Server中的存儲過程。 – Arraylist 2015-03-02 20:30:49
SQL Server _ _ _ _? – 2015-03-02 20:42:49