供应总数达到需求总数时的sql?

zpjtge22  于 2021-07-24  发布在  Java
关注(0)|答案(3)|浏览(319)

我想在SQLServer2014中从两个表生成一个查询,它们之间没有任何关系。
第一个代表要求。第二个代表他们的补给。

Demands(
    [DemandId] [int] NOT NULL,
    [ItemCode] [nvarchar](50) NULL,
    [TotalCount] [int] NULL,
    [Date] [datetime] NULL)

Supplies(
    [SupplyId] [int] NOT NULL,
    [ItemCode] [nvarchar](50) NULL,
    [Count] [int] NULL,
    [Date] [datetime] NULL)

例如,我们有一个 (TotalCount = 1000, ItemCode = 1, Date = d1) 还有两份补给品 (Date = d2, Count = 300, ItemCode = 1) 以及 (Date = d3, Count = 700, ItemCode = 1) 需求在年内完成 d3 Date ,所以我需要一个查询来指示供应品何时完成了需求。
考虑以下数据:

结果应该是:

Item01 2020-01-07

Item02 2020-01-06

谢谢你的帮助。

8zzbczxx

8zzbczxx1#

一个简单的总结可以是。。。
将需求视为负的供给量
将两个数据集合并为一个时间序列
使用累计总和查看净可用性
比如。。。

WITH
  NetContribution AS
(
  SELECT [ItemCode], [Date],  [Count]      FROM Supplies
  UNION ALL
  SELECT [ItemCode], [Date], -[TotalCount] FROM Demands
)
SELECT
  [ItemCode],
  [Date],
  [Count]       AS NetAvailabilityChange,
  SUM([Count])
    OVER (PARTITION BY [ItemCode]
              ORDER BY [Date],
                       [Count] DESC
         )
                AS NetAvailability
FROM
  NetContribution

尽管网络可用性为负,但供应尚未满足需求。虽然是正面的,但供过于求。
编辑:在回答你的问题编辑。。。
只需使用上面的查询并添加 WHERE 条款。。。

WITH
  NetContribution AS
(
  SELECT [ItemCode], [Date],  [Count]      FROM Supplies
  UNION ALL
  SELECT [ItemCode], [Date], -[TotalCount] FROM Demands
),
  NetAvailability AS
(
  SELECT
    [ItemCode],
    [Date],
    [Count]       AS Delta,
    SUM([Count])
      OVER (PARTITION BY [ItemCode]
                ORDER BY [Date],
                         [Count] DESC
           )
                  AS Amount
  FROM
    NetContribution
)
SELECT
  *
FROM
  NetAvailability
WHERE
  Amount >= 0
dba5bblo

dba5bblo2#

这是我的原始数据
需求:
'1'、'a'、'1000'、'2020-12-01'
'4'、'b'、'2000'、'2020-12-01'
供应:
'2'、'a'、'700'、'2020-12-05'
'3'、'a'、'300'、'2020-12-08'
'5'、'b'、'1000'、'2020-12-05'
'6'、'b'、'1000'、'2020-12-08'
执行了以下查询:

select a.itemcode, case when totaldemand - totalsupply = 0 then endsupplydate 
else null end enddate from </b> 
(
select 'demand' type,itemcode,sum(quantity) totaldemand,min(demanddate) as 
date from demand b group by type,itemcode ) b
inner join (
select 'supply' type,itemcode,sum(quantity) totalsupply,max(supplydate) as 
endsupplydate from supply group by type,itemcode) a
on a.itemcode = b.itemcode;

您将获得的输出:
项目代码,需求开始,供应结束,数量结束
“a”,“2020-12-08”
“b”,“2020-12-08”

disbfnqx

disbfnqx3#

在没有使用 SUM() OVER() 要生成累积和,可以使用三角形连接(将当前行连接到前面的所有行),但在大型数据集上,速度非常慢。。。

WITH
  NetContribution AS
(
  SELECT [ItemCode], [Date], SUM([Count]) AS [Count]
    FROM (
           SELECT [ItemCode], [Date],  [Count]      FROM Supplies
           UNION ALL
           SELECT [ItemCode], [Date], -[TotalCount] FROM Demands
         )
          combined
GROUP BY [ItemCode], [Date]
),
  NetAvailability AS
(
  SELECT
    a.[ItemCode],
    a.[Date],
    a.[Count]       AS Delta,
    SUM(b.[Count])  AS Amount
  FROM
    NetContribution AS a
  INNER JOIN
    NetContribution AS b
      ON  a.[ItemCode]  = b.[ItemCode]
      AND a.[Date]     >= b.[Date]
  GROUP BY
    a.[ItemCode],
    a.[Date],
    a.[Count]
)
SELECT
  *
FROM
  NetAvailability
WHERE
  Amount >= 0

https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=48660224fc63bcb2803f5a08b8b1311e

相关问题