两个日期范围之间的数量之和
我有两个表 - tab1和tab2。
TAB1包含
项目和日期
|Item|Dates |
|I1 |06-30-2022|
|I1 |07-02-2022|
|I1 |07-05-2022|
TAB2包含项目,QTY和ITEMAVAILDATE
|Item|Qty|ItemAvailDate|
|I1 |10 |06-30-2022|
|I1 |20 |07-01-2022|
|I1 |40 |07-02-2022|
|I1 |30 |07-03-2022|
|I1 |40 |07-04-2022|
|I1 |50 |07-05-2022|
我希望Tab2的数量在TAB1中的日期范围内求和,
|Item|Dates |Total(Qty)|
|I1 |06-30-2022|30 |
|I1 |07-02-2022|110 |
|I1 |07-05-2022|50 |
即从表格中的第一次日期开始-06-30-2022到下一个日期,TAB1-07-02-2022(不包括07-02)
第2行的总和为07-02-2022到07-05-2022(不包括07-05)
row3的数量总和为07-05-2022,因为此之后没有其他行
tab1中第一个日期的所有tab2的所有QTY都应求和到TAB1中的下一个日期(不包括该约会日期)。
日期应由TAB1中的可用项目划分
我不想使用循环。使用Oracle的分析函数?
任何简单的逻辑 提前致谢!
I have two tables- Tab1 and Tab2.
Tab1 contains
Item and Dates
|Item|Dates |
|I1 |06-30-2022|
|I1 |07-02-2022|
|I1 |07-05-2022|
Tab2 contains Item ,Qty and ItemAvailDate
|Item|Qty|ItemAvailDate|
|I1 |10 |06-30-2022|
|I1 |20 |07-01-2022|
|I1 |40 |07-02-2022|
|I1 |30 |07-03-2022|
|I1 |40 |07-04-2022|
|I1 |50 |07-05-2022|
I want the quantities from Tab2 to be summed up in the range of the dates in Tab1, i.e,
|Item|Dates |Total(Qty)|
|I1 |06-30-2022|30 |
|I1 |07-02-2022|110 |
|I1 |07-05-2022|50 |
Row 1 has sum of qty from first date in Tabl - 06-30-2022 till next date in Tab1 - 07-02-2022(excluding 07-02)
Row 2 has sum of qty from 07-02-2022 till 07-05-2022(excluding 07-05)
Row3 has sum of qty of 07-05-2022 as no other rows after this
All qty of Tab2 from the first date in Tab1 should be summed up till the next date in Tab1(excluding that date).
The dates should be partitioned by the items available in Tab1
I do not want to use Loops. Any easy logic using Analytical function of oracle ?
Thanks in Advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
LEAD
和一个相关的子查询:对于示例数据:
输出:
db<小提琴= AFB2E6B7A8E4CA768B8AF95C53AB897B“ rel =“ nofollow noreferrer”>此处
Use
LEAD
and a correlated sub-query:Which, for the sample data:
Outputs:
db<>fiddle here
在Oracle 12.1及更高版本中,
match_recognize
可以快速完成此类作业的工作。第一个联合所有
来自两个表的行,分配null
从表1的行中分配数量,并跟踪该行来自哪个表, 。然后,一切都准备好用于match_recognize
的微不足道应用:In Oracle 12.1 and later,
match_recognize
can do quick work of such assignments. FirstUNION ALL
the rows from both tables, assigningnull
quantity for rows from table 1, and keeping track of which table the rows come from in an additional columntbl
. Then everything is ready for a trivial application ofmatch_recognize
: