首页 > 解决方案 > 根据特定材料从同一列中选择每第 2、第 4 行等

问题描述

在 1 个包装中得到 2 个材料,项目序列为 00010 和 00020。我需要的是,如果我在 where 语句中输入 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

标签: sqlsql-serversql-server-2008

解决方案


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

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

推荐阅读