2014-08-29 69 views
1

我的任務是編寫一個允許用戶搜索價格表的應用程序,其中價格在3個不同的鍵上唯一,例如狀態出版商,和(可能有任何數量的與任何3個字段的相同的密鑰值的行,但僅存在一個行與狀態 =「俄亥俄」,出版商 =」 Bob',並且類型 ='硅')。當用戶選擇狀態出版商,他們提出的所有類型狀態出版商的列表。我運行一個存儲過程來提取這些項目,並且我拉取最近的價格,但是我也需要拉第二個最近的價格,並進行數學計算,以便將價格變化顯示給用戶。目前,我創建了以下函數,但是它會使我的存儲過程減慢1到40秒,具體取決於服務器在執行時的心情。獲取T-SQL中所有最新價格的價格變化

BEGIN 
    -- Declare the return variable here 
    DECLARE @priceChange float 
    DECLARE @currentPriceDate date 
    DECLARE @currentPrice float 
    DECLARE @previousPrice float 

    -- Add the T-SQL statements to compute the return value here 
    SELECT TOP 1 @currentPriceDate=PriceDate ,@CurrentPrice=MarketPrice 
     FROM MarketPrice_Table 
      LEFT JOIN PriceEffectiveDate_Table ON MarketPrice_Table.PriceDate = PriceEffectiveDate_Table.EffectiveDate 
       AND MarketPrice_Table.PublisherID = PriceEffectiveDate_Table.PublisherID 
     WHERE TypeID = @TypeID 
     AND MarketPrice_Table.PublisherID = @PublisherID 
     AND MarketPrice_Table.StateID = @StateID 
     ORDER BY PriceDate DESC; 

    SET @previousPrice = (SELECT TOP 1 MarketPrice 
     FROM MarketPrice_Table 
      LEFT JOIN PriceEffectiveDate_Table ON MarketPrice_Table.PriceDate = PriceEffectiveDate_Table.EffectiveDate 
       AND MarketPrice_Table.PublisherID = PriceEffectiveDate_Table.PublisherID 
     WHERE TypeID = @TypeID 
     AND MarketPrice_Table.PublisherID = @PublisherID 
     AND MarketPrice_Table.StateID = @StateID 
     AND MarketPrice_Table.PriceDate <> @currentPriceDate 
     ORDER BY PriceDate DESC); 

    SET @priceChange = @currentPrice - @previousPrice; 

    -- Return the result of the function 
    RETURN @priceChange 

END 

是否有更有效的方法來做到這一點,所以我不在存儲過程中每行兩個查詢?

非常感謝您的幫助,如果我能進一步澄清,請告訴我!

+0

首先,我建議你轉換日期字符串轉換(VARCHAR,MarketPrice_Table.PriceDate,112)<>轉換(VARCHAR,@ currentPriceDate,112) – MelgoV 2014-08-29 19:14:23

回答

0

嘗試使用LEAD分析功能和使用它在一個表中返回的數據。我很抱歉,如果這不是確切的,但有一些修改,我相信它會給你你想要的。

嘗試:

DECLARE @priceChange float 
DECLARE @currentPriceDate date 
DECLARE @currentPrice float 
DECLARE @previousPrice FLOAT 

SELECT 
    * 
FROM 
    ( 
     SELECT 
      ROW_NUMBER() OVER (PARTITION BY MarketPrice_Table.StateID, MarketPrice_Table.PublisherID, TypeID ORDER BY PriceDate DESC) AS RowNum, 
      MarketPrice_Table.StateID, 
      MarketPrice_Table.PublisherID, 
      TypeID, 
      PriceDate, 
      MarketPrice AS CurrentPrice, 
      LEAD(MarketPrice) OVER (PARTITION BY MarketPrice_Table.StateID, MarketPrice_Table.PublisherID, TypeID ORDER BY PriceDate DESC) AS PreviousPrice, 
      MarketPrice - ISNULL(LEAD(MarketPrice) OVER (PARTITION BY MarketPrice_Table.StateID, MarketPrice_Table.PublisherID, TypeID ORDER BY PriceDate DESC), 0) AS PriceChange 
     FROM 
      MarketPrice_Table 
      LEFT JOIN PriceEffectiveDate_Table 
       ON MarketPrice_Table.PriceDate = PriceEffectiveDate_Table.EffectiveDate 
       AND MarketPrice_Table.PublisherID = PriceEffectiveDate_Table.PublisherID 
     WHERE 
      TypeID = @TypeID AND 
      MarketPrice_Table.PublisherID = @PublisherID AND 
      MarketPrice_Table.StateID = @StateID 
    ) r 
WHERE 
    r.RowNum = 1 
+0

我目前正在執行這個方法,它似乎真的在快速工作。雖然它不會返回CurrentPrice和PreviousPrice之間的差異,所以我試圖弄清楚如何從中獲得它。如果我能做到這一點,我會接受它作爲答案。非常感謝你! – user1543432 2014-08-29 20:10:29

+0

查看編輯答案。 – Ahz 2014-08-29 20:12:48

+0

完美的工作!謝謝!在我閱讀你的回覆之前,我把'Select *'改成了'Select CurrentPrice-PreviousPrice',它也注意到我之後的結果。但在查看執行時間後,您的版本更快。再次感謝你!我一整天都在撓頭! – user1543432 2014-08-29 20:28:38

0

試試這個請:

BEGIN 
-- Declare the return variable here 
DECLARE @priceChange float 
DECLARE @currentPriceDate varchar(8) 
DECLARE @currentPrice float 
DECLARE @previousPrice float 

-- Add the T-SQL statements to compute the return value here 
SELECT TOP 1 @currentPriceDate=Convert(varchar,PriceDate,112) ,@CurrentPrice=MarketPrice 
    FROM MarketPrice_Table 
     LEFT JOIN PriceEffectiveDate_Table ON Convert(varchar,MarketPrice_Table.PriceDate,112) = Convert(varchar,PriceEffectiveDate_Table.EffectiveDate,112) 
      AND MarketPrice_Table.PublisherID = PriceEffectiveDate_Table.PublisherID 
    WHERE TypeID = @TypeID 
    AND MarketPrice_Table.PublisherID = @PublisherID 
    AND MarketPrice_Table.StateID = @StateID 
    ORDER BY PriceDate DESC; 

SET @previousPrice = (SELECT TOP 1 MarketPrice 
    FROM MarketPrice_Table 
     LEFT JOIN PriceEffectiveDate_Table ON Convert(varchar,MarketPrice_Table.PriceDate,112) = Convert(varchar,PriceEffectiveDate_Table.EffectiveDate) 
      AND MarketPrice_Table.PublisherID = PriceEffectiveDate_Table.PublisherID 
    WHERE TypeID = @TypeID 
    AND MarketPrice_Table.PublisherID = @PublisherID 
    AND MarketPrice_Table.StateID = @StateID 
    AND Convert(varchar,MarketPrice_Table.PriceDate,112) <> @currentPriceDate 
    ORDER BY PriceDate DESC); 

SET @priceChange = @currentPrice - @previousPrice; 

-- Return the result of the function 
RETURN @priceChange 

END