如何获得最大序列号?

时间:2019-05-31 18:05:01

标签: sql db2 dense-rank

这是我上一篇文章的延续:here

我有这个查询:

SELECT INVOICE_NUMBER, INVOICE_SEQ_NUMBER, FILE_NUMBER
FROM (SELECT A.INVOICE_NUMBER, A.INVOICE_SEQ_NUMBER, B.FILE_NUMBER,
             DENSE_RANK() OVER (ORDER BY A.INVOICE_NUMBER) as seqnum
      FROM TABLE1 A JOIN
           TABLE2 B 
           ON A.INVOICE_NUMBER = B.INVOICE_NUMBER AND 
              A.INVOICE_SEQ_NUMBER = B.INVOICE_SEQ_NUMBER
     ) t
WHERE seqnum <= 3;

结果如下:

-----------------------------------------------------
| INVOICE_NUMBER | INVOICE_SEQ_NUMBER | FILE_NUMBER |
------------------------------------------------------
|1111111111-1    |          1         | P4324324525 |
-----------------------------------------------------
|1111111111-1    |          2         | P4565674574 |
-----------------------------------------------------
|1111111111-1    |          3         | V4324552557 |
-----------------------------------------------------
|1111111111-1    |          4         | V4324552525 |
-----------------------------------------------------
|2222222222-2    |          1         | S4563636574 |
-----------------------------------------------------
|3333333333-3    |          1         | Q4324325675 |
-----------------------------------------------------
|3333333333-3    |          2         | Q4565674574 |
-----------------------------------------------------

因此,新的要求是如何获取相同发票编号的最大发票序列号?结果应该是这样的:

------------------------------------------------------------------------
| INVOICE_NUMBER | INVOICE_SEQ_NUMBER | FILE_NUMBER |MAX_INV_SEQ_NUMBER|
------------------------------------------------------------------------
|1111111111-1    |          1         | P4324324525 |        4         |
------------------------------------------------------------------------
|1111111111-1    |          2         | P4565674574 |        4         |
------------------------------------------------------------------------
|1111111111-1    |          3         | V4324552557 |        4         |
------------------------------------------------------------------------
|1111111111-1    |          4         | V4324552525 |        4         |
------------------------------------------------------------------------
|2222222222-2    |          1         | S4563636574 |        1         |
------------------------------------------------------------------------
|3333333333-3    |          1         | Q4324325675 |        2         |
------------------------------------------------------------------------
|3333333333-3    |          2         | Q4565674574 |        2         |
------------------------------------------------------------------------

3 个答案:

答案 0 :(得分:0)

在选择部分中添加一个额外的列,如下所示-

SELECT 
INVOICE_NUMBER, 
INVOICE_SEQ_NUMBER, 
FILE_NUMBER,
(
    SELECT COUNT(*) 
    FROM TABLE1 A 
    JOIN TABLE2 B 
    ON A.INVOICE_NUMBER = B.INVOICE_NUMBER 
    AND A.INVOICE_SEQ_NUMBER = B.INVOICE_SEQ_NUMBER
    AND A.INVOICE_NUMBER = t.INVOICE_NUMBER 
)MAX_INV_SEQ_NUMBER
FROM  ........

答案 1 :(得分:0)

SELECT INVOICE_NUMBER, INVOICE_SEQ_NUMBER, FILE_NUMBER, MAX(INVOICE_SEQ_NUMBER) OVER (PARTITION BY INVOICE_NUMBER)
FROM (SELECT A.INVOICE_NUMBER, A.INVOICE_SEQ_NUMBER, B.FILE_NUMBER,
             DENSE_RANK() OVER (ORDER BY A.INVOICE_NUMBER) as seqnum
      FROM TABLE1 A JOIN
           TABLE2 B 
           ON A.INVOICE_NUMBER = B.INVOICE_NUMBER AND 
              A.INVOICE_SEQ_NUMBER = B.INVOICE_SEQ_NUMBER
     ) t
WHERE seqnum <= 3;

本质上,您只需要在select语句中使用它即可

MAX(INVOICE_SEQ_NUMBER) OVER (PARTITION BY INVOICE_NUMBER)

答案 2 :(得分:0)

将以下表达式添加到select列表中:
, max(INVOICE_SEQ_NUMBER) over (partition by INVOICE_NUMBER) as MAX_INV_SEQ_NUMBER