Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 查询上次unt价格之前的详细记录 样品 预期_Sql_Sql Server - Fatal编程技术网

Sql 查询上次unt价格之前的详细记录 样品 预期

Sql 查询上次unt价格之前的详细记录 样品 预期,sql,sql-server,Sql,Sql Server,如果查询条件为订单日期介于2020-02-02和2020-02-10之间,则数据预计会低于结果 +---------+------------+----------+------------+------------------------+-----------------+-------------+-----------------------------+ | prdt_no | order_date | quantity | unit_price | last_unit_price_b

如果查询条件为订单日期介于2020-02-02和2020-02-10之间,则数据预计会低于结果

+---------+------------+----------+------------+------------------------+-----------------+-------------+-----------------------------+
| prdt_no | order_date | quantity | unit_price | last_unit_price_before | unit_price_diff | cost_reduce | last_unit_price_change_date |
+---------+------------+----------+------------+------------------------+-----------------+-------------+-----------------------------+
| A001    | 2020-02-05 |      100 |         20 |                     10 |              10 |        1000 | 2020-02-01                  |
| A001    | 2020-02-07 |      100 |         20 |                     10 |              10 |        1000 | 2020-02-01                  |
| A001    | 2020-02-10 |      100 |         15 |                     20 |              -5 |        -500 | 2020-02-10                  |
| A002    | 2020-02-05 |      100 |         20 |                     10 |              10 |        1000 | 2020-02-01                  |
| A002    | 2020-02-07 |      100 |         20 |                     10 |              10 |        1000 | 2020-02-01                  |
| A002    | 2020-02-10 |      100 |         15 |                     20 |              -5 |        -500 | 2020-02-10                  |
+---------+------------+----------+------------+------------------------+-----------------+-------------+-----------------------------+
思维方式 我希望得到相同产品的最后单价,然后用它来计算差价 数据记录计数实际上超过了200K 喜欢照片

测试演示链接 您可以使用OUTER APPLY获得差价的最低价

SELECT *,
       unit_price_diff = T.[unit_price] - L.[last_unit_price_before]
FROM   T
       OUTER APPLY
       (
           SELECT TOP 1 
                  last_unit_price_before = x.[unit_price],
                  last_unit_price_change_date = x.[order_date]
           FROM   T x
           WHERE  x.[prdt_no] = T.[prdt_no]
           AND    x.[order_date] < T.[order_date]
           AND    x.[unit_price] <> T.[unit_price]
           ORDER BY x.[order_date] DESC
       ) L
WHERE  T.[order_date] >= '2020-02-01'
AND    T.[order_date] <= '2020-02-10'

为什么你的专栏里有20多岁?@GordonLinoff 2020-XX-XX?这是Tawn日期格式。比如XX/XX/2020。前一列最后一个单位价格。@GordonLinoff,例如2020-02-10的A001价格是15,但2020-02-07的最后一个价格是20为什么2020-02-01不在结果中?我没有包括成本减少,因为我不确定计算方法
SELECT *,
       unit_price_diff = T.[unit_price] - L.[last_unit_price_before]
FROM   T
       OUTER APPLY
       (
           SELECT TOP 1 
                  last_unit_price_before = x.[unit_price],
                  last_unit_price_change_date = x.[order_date]
           FROM   T x
           WHERE  x.[prdt_no] = T.[prdt_no]
           AND    x.[order_date] < T.[order_date]
           AND    x.[unit_price] <> T.[unit_price]
           ORDER BY x.[order_date] DESC
       ) L
WHERE  T.[order_date] >= '2020-02-01'
AND    T.[order_date] <= '2020-02-10'