Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取T-SQL中所有最新价格的价格变化_Sql_Sql Server_Tsql_Sql Server 2012_Sql Function - Fatal编程技术网

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

获取T-SQL中所有最新价格的价格变化,sql,sql-server,tsql,sql-server-2012,sql-function,Sql,Sql Server,Tsql,Sql Server 2012,Sql Function,我的任务是编写一个应用程序,允许用户搜索一个价格表,其中价格在3个不同的键上是唯一的,比如state、publisher和type。对于这3个字段中的任何一个,可能有任意数量的行具有相同的键值,但只有一行state='Ohio',publisher='Bob',和type='silicon'。当用户选择“状态”和“发布者”时,系统会向其显示一个包含该状态和“发布者”的所有类型的列表。我运行一个存储过程来提取这些项目,我提取的是最新的价格,但我还需要提取第二个最新的价格,并进行数学运算,以获得价格

我的任务是编写一个应用程序,允许用户搜索一个价格表,其中价格在3个不同的键上是唯一的,比如state、publisher和type。对于这3个字段中的任何一个,可能有任意数量的行具有相同的键值,但只有一行state='Ohio',publisher='Bob',和type='silicon'。当用户选择“状态”和“发布者”时,系统会向其显示一个包含该状态和“发布者”的所有类型的列表。我运行一个存储过程来提取这些项目,我提取的是最新的价格,但我还需要提取第二个最新的价格,并进行数学运算,以获得价格的变化以显示给用户。目前,我创建了以下函数,但它会将存储过程的速度降低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
是否有更有效的方法来执行此操作,这样我就不会在存储过程中每行进行两次查询

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

请试试这个:

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

尝试使用LEAD分析函数,并使用它返回表中的数据。我很抱歉,如果这不准确,但经过一些修改,我相信它会给你你想要的

尝试:


首先,我建议您将日期转换为字符串Convertvarchar,MarketPrice_Table.PriceDate,112 Convertvarchar,@currentPriceDate,112。我目前正在实现这个方法,它似乎工作得很快。但它不会返回当前价格和以前价格之间的差异,所以我正试图找出如何从中得到它。我会接受它作为答案,如果我能让这个工作。非常感谢你!那很有效!非常感谢。在阅读您的回复之前,我将Select*更改为Select CurrentPrice PreviousPrice,这也注意到了我所追求的结果。但在查看执行时间后,您的版本会更快。再次感谢你!我一整天都在为这个挠头!
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