繁体   English   中英

根据特定材料,从同一列中选择第二,第四行等

[英]Select every 2nd, 4th row and so on from same column based on specific material

在1个包装中,有2个物料,物料序列为00010和00020。我需要的是,如果我从物料序列00010中的物料输入到“ CB016”语句中,则可以列出所有物料序列00020。

表数据

Packing         ItemSeq ItemCate    Material    TargetQty   MinQty
1000009654      10      P           CB016       1            0
1000009654      20      I           10000015991 48           0
1000012548      10      P           CB016       1            0
1000012548      20      I           10000009495 48           0
1000012564      10      P           CB016       1            0
1000012564      20      I           10000009517 48           0
1000007961      10      P           CB017       1            0
1000007961      20      I           10000003423 10000        0
1000007962      10      P           CB017       1            0
1000007962      20      I           10000003424 10000        0

预期产量

Packing         ItemSeq ItemCate    Material    TargetQty   MinQty
1000009654      20      I           10000015991 48           0
1000012548      20      I           10000009495 48           0
1000012564      20      I           10000009517 48           0

您可以尝试使用row_number()函数:

select * from
(select *, row_number() over (partition by packing order by itemseq desc) as rn)a
where rn=1

此处不需要窗口功能。 您需要20行,而同一组中有10行。

SELECT *
FROM yourdata item20
WHERE ItemSeq = 20
AND EXISTS (
    SELECT 1
    FROM yourdata item10
    WHERE item10.Packing = item20.packing
    AND ItemSeq = 10
    AND Material = 'CB016' -- insert material name here
)

DB小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM