SQL查询:仅基于交易数据的每日股票摘要

huangapple go评论67阅读模式
英文:

SQL Query for Stock Summary Per Date Based Only on Transactional Data

问题

我需要制作一个仅基于交易数据表的库存报告。

例如,我有这样的表格,其中ProductType确定库存是进还是出(+表示进货,-表示出货)

产品ID 数量 产品类型 日期
1 630 1 2021-10-11
1 630 -1 2021-10-11
1 630 1 2021-10-12
1 630 1 2021-10-12
1 430 -1 2021-10-13

基于输入的参数,即产品ID、开始日期和结束日期,我希望根据数据制作如下表格,假设开始数量为0。

产品ID 日期 开始数量 进货数量 出货数量 结束数量
1 2021-10-11 0 630 0 630
1 2021-10-11 630 0 630 0
1 2021-10-12 630 630 0 630
1 2021-10-12 630 630 0 1260
1 2021-10-13 1260 0 430 830

非常感谢您的帮助!谢谢!

英文:

I have an assignment to make an inventory report only based on the transactional data table.

For example, I have the table like this, where ProductType determines whether the stock is going in or out (+ for stock in and - for out)

ProductID Quantity ProductType Date
1 630 1 2021-10-11
1 630 -1 2021-10-11
1 630 1 2021-10-12
1 630 1 2021-10-12
1 430 -1 2021-10-13

And based on the data I want to make it look like this based on the parameter inputted which is productid, beginning date, and end date. Assuming the beginning quantity is 0.

ProductID Date Beginning Quantity Quantity In Quantity Out Ending Quantity
1 2021-10-11 0 630 0 630
1 2021-10-11 630 0 630 0
1 2021-10-12 630 630 0 630
1 2021-10-12 630 630 0 1260
1 2021-10-13 1260 0 430 830

Any help will be much appreciated, thank you!

答案1

得分: 1

以下是翻译好的内容:

一个示例如何执行此操作。如果您有交易ID,则请使用它来代替row_number(),因为这只是一种在一天内拆分金额的方法。

create table Products (ProductID int, Quantity int, ProductType int, [Date] datetime)
insert into Products
values
(1, 630, 1, '2021-10-11'),
(1, 630, -1, '2021-10-11'),
(1, 630, 1, '2021-10-12'),
(1, 630, 1, '2021-10-12'),
(1, 430, -1, '2021-10-13')

declare @productID int = 1,
        @Beg_Dte date = '2021-10-01',
		@End_Dte date = '2021-10-31';

with input_data as(
select 
  row_number()over(partition by ProductID order by [Date]) as ID, 
  ProductID, 
  [Date], 
  Quantity*ProductType as Qty,
  iif(ProductType>0,Quantity,0) as [Quantity In],
  iif(ProductType<0,Quantity,0) as [Quantity Out],
  lag(Quantity*ProductType,1,0)over(partition by ProductID order by ProductID,[Date]) as Beg
from Products
)
select ID, ProductID, [Date], 
       sum(Beg)over(partition by ProductID order by ID) as [Beginning Quantity], 
       [Quantity In], 
       [Quantity Out], 
       sum(Beg)over(partition by ProductID order by ID)+Qty as [Ending Quantity] 
from input_data
where ProductID = @productID
  and [Date] between @Beg_Dte and @End_Dte

另外,递归CTE或CASE语句也应该起作用。

如果您没有交易ID,请考虑结果集的另一种表示形式。也许更好的做法是每天只显示一行。这样更容易组织。类似于以下内容:

ProductID Date Beginning Quantity Quantity In Quantity Out Ending Quantity
1 2021-10-11 0 630 630 0
1 2021-10-12 0 1260 0 1260
1 2021-10-13 1260 0 430 830

顺便说一句,这一行存在错误。Beginning Quantity 应该为零。

ProductID Date Beginning Quantity Quantity In Quantity Out Ending Quantity
1 2021-10-12 630 630 0 630
英文:

One example of how to do this. If you have transaction ID then use it instead of row_number() because it is just a way how to split the amounts within one day.

create table Products (ProductID int, Quantity int, ProductType int, [Date] datetime)
insert into Products
values
(1,	630,	1,	&#39;2021-10-11&#39;),
(1,	630,	-1,	&#39;2021-10-11&#39;),
(1,	630,	1,	&#39;2021-10-12&#39;),
(1,	630,	1,	&#39;2021-10-12&#39;),
(1,	430,	-1,	&#39;2021-10-13&#39;)

declare @productID int = 1,
        @Beg_Dte date = &#39;2021-10-01&#39;,
		@End_Dte date = &#39;2021-10-31&#39;;

with input_data as(
select 
  row_number()over(partition by ProductID order by [Date]) as ID, 
  ProductID, 
  [Date], 
  Quantity*ProductType as Qty,
  iif(ProductType&gt;0,Quantity,0) as [Quantity In],
  iif(ProductType&lt;0,Quantity,0) as [Quantity Out],
  lag(Quantity*ProductType,1,0)over(partition by ProductID order by ProductID,[Date]) as Beg
from Products
)
select ID, ProductID, [Date], 
       sum(Beg)over(partition by ProductID order by ID) as [Beginning Quantity], 
       [Quantity In], 
       [Quantity Out], 
       sum(Beg)over(partition by ProductID order by ID)+Qty as [Ending Quantity] 
from input_data
where ProductID = @productID
  and [Date] between @Beg_Dte and @End_Dte

SQL查询:仅基于交易数据的每日股票摘要

In addition, recursive CTE or CASE statements also should work.

If you do not have transaction ID, think about the another representation of the result set. Maybe it is better to show only one row for each day. It should be easier to organise. Something like this:

ProductID Date Beginning Quantity Quantity In Quantity Out Ending Quantity
1 2021-10-11 0 630 630 0
1 2021-10-12 0 1260 0 1260
1 2021-10-13 1260 0 430 830

By the way, there is an error in this line. Beginning Quantity should be zero.

ProductID Date Beginning Quantity Quantity In Quantity Out Ending Quantity
1 2021-10-12 630 630 0 630

huangapple
  • 本文由 发表于 2023年6月19日 10:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定