获取T-SQL中所有最新价格的价格变化

时间:2014-08-29 19:04:05

标签: sql sql-server tsql sql-server-2012 sql-function

我的任务是编写一个应用程序,允许用户搜索价格表,其中价格在3个不同的键上是唯一的,例如发布商< / strong>,和类型(对于3个字段中的任何一个,可能有任意数量的行具有相同的键值,但只有一行具有状态 =& #39;俄亥俄州&#39;,发布商 =&#39; Bob&#39;和类型 =&#39; silicon&#39;)。当用户选择发布商时,系统会显示包含的所有类型的列表和发布者。我运行一个存储过程来提取这些项目,我正在提取最近的价格,但我还需要提取第二个最近的价格并做数学以获得价格变化以显示给用户。目前,我创建了以下函数,但它会使我的存储过程减慢1到40秒,具体取决于服务器执行时的情绪。

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

是否有更有效的方法来执行此操作,因此我不会在存储过程中每行进行两次查询?

提前感谢您的帮助,如果我能进一步澄清,请告诉我们。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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