1

我试图达到以下结果:

ID |  Part  | QTY| Boxes| Reference
1  | ABC123 | 20 | 0    | REF0001
2  | ABC345 | 10 | 0    | REF0001
3  | ABC487 | 5  | 1    | REF0001
4  | SEF453 | 4  | 0    | REF0002
5  | ABDS12 | 82 | 4    | REF0002
6  | EFR488 | 64 | 0    | REF0003
7  | XCV345 | 58 | 0    | REF0003
8  | SSFS33 | 23 | 3    | REF0003

现在我得到

ID |  Part  | QTY| Boxes| Reference
1  | ABC123 | 20 | 1    | REF0001
2  | ABC345 | 10 | 1    | REF0001
3  | ABC487 | 5  | 1    | REF0001
4  | SEF453 | 4  | 4    | REF0002
5  | ABDS12 | 82 | 4    | REF0002
6  | EFR488 | 64 | 3    | REF0003
7  | XCV345 | 58 | 3    | REF0003
8  | SSFS33 | 23 | 3    | REF0003

如您所见,每个引用的框数量重复每一行,并且每个引用我只需要出现一个。

4

2 回答 2

0

分配每个引用分组的行号会将共享相同引用的最高 ID 标记为 1;主查询检查此标记,如果不满足则输出零。

; with q as
(
  select *,
         row_number() over (partition by Reference
                            order by ID desc) rn
    from
    (
        your-query-here
    ) a
)
select q.ID, 
       q.Part, 
       q.QTY, 
       case when rn = 1 then q.Boxes else 0 end as Boxes, 
       q.Reference
  from q
 order by q.ID
于 2012-07-17T00:29:21.833 回答
0

好吧,这是一种方法。. .

with t as (<your current query>)
select ID, Part, QTY,
       max(Boxes) over (partition by Reference) as Boxes,
       Reference
from t
于 2012-07-17T00:01:08.690 回答