【发布时间】:2020-06-02 16:01:59
【问题描述】:
我正在服务器上学习 SQL:IBM V7R1M0、DB2。
我正在尝试构建 SQL 报告。 找了几天类似的例子,我在知识的海洋里推出了这个瓶子……
上下文: 商店向仓库索取货物。 这些货物是在托盘上挑选的。 这些托盘在装载到卡车之前将被放置在临时通道上。
规则 1:我们只希望一个商店的货盘在集结通道上(我们不想混合来自不同商店的货盘)
规则 2:商店将占用附近的集结通道。
规则 3:暂存通道按那里的 ID 排序(有间隙)
表 1:
|-----|-----|-----------------|
| ID |store|pallet_estimation|
|-----|-----|-----------------|
| 1 | A | 35 |
| 2 | C | 2 |
| 3 | B | 30 |
|-----|-----|-----------------|
SELECT * FROM (
VALUES (1, 'A', 35), (2, 'C', 2), (3, 'B', 30)
) T1(ID, store, pallet_estimation)
表 2:
|---------------|---------------|
|ID_staging_lane|pallet_capacity|
|---------------|---------------|
| 201 | 10 |
| 202 | 10 |
| 204 | 30 |
| 205 | 40 |
| 208 | 30 |
| 210 | 30 |
|---------------|---------------|
SELECT * FROM(
VALUES (201, 10), (202, 10), (204, 30), (205, 40), (208, 30), (210, 30)
) T2(ID_staging_lane, pallet_capacity)
预期结果:
|-----------|--------|--------------------|---------------|------------------|
|T1_sequence|T1_store|T1_pallet_estimation|T2_staging_lane|T2_pallet_capacity|
|-----------|--------|--------------------|---------------|------------------|
| 1 | A | 35 | 201 | 10 |
| 1 | A | 35 | 202 | 10 |
| 1 | A | 35 | 204 | 30 |
| 2 | C | 2 | 205 | 40 |
| 3 | B | 30 | 208 | 30 |
|-----------|--------|--------------------|---------------|------------------|
谢谢你,Charles,抽出时间。 我会努力改善我的需求。
如果需要,我想按照顺序将pallet_estimation 拆分/划分到几个分段通道上
例子:
For store A which has 35 pallets,
I want to use staging lane 201 then it remains 35 - 10 = 25 ,
then I want to use staging lane 202 then it remains 25 - 10 = 15,
then I want to use staging lane 204 then it remains 15 - 30 = -15
then I want to continue with the store C on the next staging lane 205 then it remains 2 - 40 = -38
then I want to continue with the store B on the next staging lane 208 then it remains 30 - 30 = 0
您将如何开始构建它? - 有窗口功能?总和()超过() - 使用递归 SQL 吗?声明获取 - 是否可以在 SQL 中构建动态 JOIN ? - 其他想法?
提前致谢,
雷诺
【问题讨论】: