Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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
Sql server SQL Server使用查询计算运行总值_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql server SQL Server使用查询计算运行总值

Sql server SQL Server使用查询计算运行总值,sql-server,sql-server-2012,Sql Server,Sql Server 2012,我使用SQL Server 2012,其表如下所示: DECLARE @T TABLE(Id INT, [Type] CHAR(1), Quantity INT, Price MONEY, UnitPrice AS (Price/Quantity)) INSERT INTO @T VALUES (1, 'I', 30, 1500), (2, 'O', 5, NULL), (3, 'O', 20, NULL), (4, 'O', 2, NULL), (5,

我使用SQL Server 2012,其表如下所示:

DECLARE @T TABLE(Id INT, [Type] CHAR(1), Quantity INT, Price MONEY, UnitPrice AS (Price/Quantity))
INSERT INTO @T VALUES 
    (1, 'I', 30, 1500),
    (2, 'O', 5, NULL),
    (3, 'O', 20, NULL),
    (4, 'O', 2, NULL),
    (5, 'I', 10, 2500),
    (6, 'I', 8, 1000),
    (7, 'O', 3, NULL),
    (8, 'O', 10, NULL),
    (9, 'I', 12, 3600)
在我的表中,我有一个类型列,其值为
('I'和'O')
我有'I'类型记录和'O'类型记录的单价,上一个'I'类型记录的值我要计算RunningTotalPrice(数量之和*每行单价)

以下代码计算RunningTotalQuantity:

SELECT *, 
        SUM(CASE WHEN [Type] = 'I' Then Quantity ELSE -Quantity END)OVER (ORDER BY Id) AS QuantityRunningTotal
FROM @T
此查询的结果为:

Id  Type    Quantity    Price   UnitPrice   QuantityRunningTotal
1   I       30          1500/00 50/00       30
2   O       5           NULL    NULL        25
3   O       20          NULL    NULL        5
4   O       2           NULL    NULL        3
5   I       10          2500/00 250/00      13
6   I       8           1000/00 125/00      21
7   O       3           NULL    NULL        18
8   O       10          NULL    NULL        8
9   I       12          3600/00 300/00      20
我想得到以下结果

Id  Type    Quantity    Price   UnitPrice   QuantityRunningTotal  Price       RunningTotalPrice
1   I       30          1500/00 50/00       30                    1500/00      1500/00
2   O       5           NULL    50/00       25                    250/00       1250/00
3   O       20          NULL    50/00       5                     1000/00      250/00
4   O       2           NULL    50/00       3                     100/00       150/00
5   I       10          2500/00 250/00      13                    2500/00      2650/00
6   I       8           1000/00 125/00      21                    1000/00      3650/00
7   O       3           NULL    125/00      18                    375/00       3275/00
8   O       10          NULL    125/00      8                     1250/00      2025/00
9   I       12          3600/00 300/00      20                    3600/00      5625/00
在此结果中,单位价格列为Null,其值为before记录中最后存在的单位价格。
和计算价格(数量*单价)和计算价格的运行总计。

不幸的是,
超前
滞后
函数不能用于最后一个非
值,因此您需要使用
外部应用
来获取前一个
单价
,以便在类型为“O”的行中使用:

SELECT  t.ID,
        t.[Type],
        t.Quantity,
        t.Price,
        t.UnitPrice, 
        SUM(CASE WHEN t.[Type] = 'I' THEN t.Quantity ELSE -t.Quantity END) OVER (ORDER BY t.Id) AS QuantityRunningTotal,
        CASE WHEN t.[Type] = 'I' THEN t.Price ELSE t.Quantity * p.UnitPrice END AS Price2,
        SUM(CASE WHEN t.[Type] = 'I' THEN t.Price ELSE -t.Quantity * p.UnitPrice END)OVER (ORDER BY t.Id) AS QuantityRunningTotal
FROM    @T AS t
        OUTER APPLY
        (   SELECT  TOP 1 t2.UnitPrice
            FROM    @T AS t2
            WHERE   t2.ID < t.ID
            AND     t2.UnitPrice IS NOT NULL
            ORDER BY t2.ID DESC
        ) AS p;
选择t.ID,
t、 [类型],
t、 数量,
t、 价格,
t、 单价,
总和(当t.[Type]=“I”然后是t.Quantity ELSE-t.Quantity END时的情况)超过(按t.Id排序)作为QuantityRunningTotal,
当t.[Type]=“I”然后t.价格其他t.数量*p.单价结束为价格2时的情况,
总计(当t.[Type]=“I”然后t.Price ELSE-t.Quantity*p.UnitPrice END时的情况)超过(按t.Id订购)作为QuantityRunningTotal
从@T作为T
外敷
(选择前1位t2.1单价)
从@T到t2
其中t2.ID
我仍然无法获得第二列
价格
按数量*单价计算的第二列价格,该价格等于“I”类型记录的第一列价格。请回答。我的原始表有超过1000万条记录,您的查询速度非常慢。我寻找一个高性能的查询。thanks@mehdilotfi子查询通常比窗口函数快。尝试将索引添加到ID。或者,您可以编写脚本来填写空值