Oracle 通过连接两个表获得最大值

发布于 2024-11-03 10:26:51 字数 2447 浏览 1 评论 0原文

Oracle通过连接两个表获得最大值我做错了什么?

tblKG:

KG_USER    KG_DATE    KG_TK_NO    KG_SQ_NO    KG_CNG_IND
---------------------------------------------------------
123456   200820     1           1           B
123456   200820     2           1           A
123456   200820     3           1           A
123456   200820     4           1           I
123456   200820     4           2           F
123456   200820     4           3           I
123456   200820     4           4           F
123456   200820     4           5           I
123456   200820     4           6           F
123456   200820     4           7           A

tblKN:

KN_USER    KN_DATE     KN_SQ_NO    KN_SEQ_NUMB
--------------------------------------------------
123456      200820      1           01
123456      200820      2           01
123456      200820      3           01
123456      200820      4           07

查看:

SELECT
        N.KN_DATE AS DATE,
        N.KN_USER AS USER,
        N.KN_SEQ_NO,    
        G.KG_TK_NO,
        G.KG_DATE,
        G.KG_USER,
        G.KGCNG_IND,                          
        G.MAX_SEQ_NO                              
 FROM               
        KN N,        
        (SELECT 
          KG_TK_NO, 
          KG_DATE,   
          KG_USER,       
          KG_CNG_IND AS KGCNG_IND,
          MAX(KG_SEQ_NO)  AS MAX_SEQ_NO        
     FROM
      KG
      GROUP BY KG_TK_NO, KG_DATE, KG_USER, KG_CNG_IND
    )  G
WHERE      
        N.KN_DATE = G.KG_DATE    
       AND N.KN_USER = G.KG_USER           
       AND N.KN_SEQ_NO = G.KG_TK_NO

结果:

DATE      USER        KN_SEQ_NO   KG_TK_NO    KGCNG_IND   MAX_SEQ_NO
200820  123456      1           1           B           1
200820  123456      2           2           A           1
200820  123456      3           3           A           1
200820  123456      4           4           A           7
200820  123456      4           4           F           6
200820  123456      4           4           I           5

预期:

DATE      USER        KN_SEQ_NO   KG_TK_NO    KGCNG_IND   MAX_SEQ_NO
200820    123456      1           1           B           1
200820    123456      2           2           A           1
200820    123456      3           3           A           1
200820    123456      4           4           A           7

Oracle Max value via join two tables what am I doing wrong?

tblKG:

KG_USER    KG_DATE    KG_TK_NO    KG_SQ_NO    KG_CNG_IND
---------------------------------------------------------
123456   200820     1           1           B
123456   200820     2           1           A
123456   200820     3           1           A
123456   200820     4           1           I
123456   200820     4           2           F
123456   200820     4           3           I
123456   200820     4           4           F
123456   200820     4           5           I
123456   200820     4           6           F
123456   200820     4           7           A

tblKN:

KN_USER    KN_DATE     KN_SQ_NO    KN_SEQ_NUMB
--------------------------------------------------
123456      200820      1           01
123456      200820      2           01
123456      200820      3           01
123456      200820      4           07

VIEW:

SELECT
        N.KN_DATE AS DATE,
        N.KN_USER AS USER,
        N.KN_SEQ_NO,    
        G.KG_TK_NO,
        G.KG_DATE,
        G.KG_USER,
        G.KGCNG_IND,                          
        G.MAX_SEQ_NO                              
 FROM               
        KN N,        
        (SELECT 
          KG_TK_NO, 
          KG_DATE,   
          KG_USER,       
          KG_CNG_IND AS KGCNG_IND,
          MAX(KG_SEQ_NO)  AS MAX_SEQ_NO        
     FROM
      KG
      GROUP BY KG_TK_NO, KG_DATE, KG_USER, KG_CNG_IND
    )  G
WHERE      
        N.KN_DATE = G.KG_DATE    
       AND N.KN_USER = G.KG_USER           
       AND N.KN_SEQ_NO = G.KG_TK_NO

RESULT:

DATE      USER        KN_SEQ_NO   KG_TK_NO    KGCNG_IND   MAX_SEQ_NO
200820  123456      1           1           B           1
200820  123456      2           2           A           1
200820  123456      3           3           A           1
200820  123456      4           4           A           7
200820  123456      4           4           F           6
200820  123456      4           4           I           5

Expected:

DATE      USER        KN_SEQ_NO   KG_TK_NO    KGCNG_IND   MAX_SEQ_NO
200820    123456      1           1           B           1
200820    123456      2           2           A           1
200820    123456      3           3           A           1
200820    123456      4           4           A           7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-11-10 10:26:51

如果您想获得最大值,请使用 MAXGROUP BY。但您并不是真的想要这样,您想要获得具有最大值的

为此,您需要根据您感兴趣的列对行进行排名,然后仅使用第一个。

SELECT * FROM
 (SELECT 
      KG_TK_NO, 
      KG_DATE,   
      KG_USER,       
      KG_CNG_IND,
      KG_SEQ_NO,
      RANK() OVER (PARTITION BY KG_TK_NO, KG_DATE, KG_USER
                   ORDER BY KG_SEQ_NO DESC) AS R
  FROM KG)
WHERE R = 1

If you want to get the maximum value, you use MAX and GROUP BY. But you don't really want that, you want to get the row with the maximum value.

For that, you need to rank the rows according to the column you are interested in, and then just use the first one.

SELECT * FROM
 (SELECT 
      KG_TK_NO, 
      KG_DATE,   
      KG_USER,       
      KG_CNG_IND,
      KG_SEQ_NO,
      RANK() OVER (PARTITION BY KG_TK_NO, KG_DATE, KG_USER
                   ORDER BY KG_SEQ_NO DESC) AS R
  FROM KG)
WHERE R = 1
绅刃 2024-11-10 10:26:51

从您的预期输出中怀疑您不需要 GROUP BY 的第四个变量。

GROUP BY KG_TK_NO, KG_DATE, KG_USER, ->KG_CNG_IND<-

并且您希望在此子句中使用 MAX_SEQ_NO 而不是 G.KG_TK_NO

N.KN_SEQ_NO = G.KG_TK_NO

顺便说一句,您的列名有点难以使用。我不认为 KG_ 前缀样式有用,尽管它很流行。但其余的名字都太短了。

Suspect from your expected output you do not want fourth variable of GROUP BY.

GROUP BY KG_TK_NO, KG_DATE, KG_USER, ->KG_CNG_IND<-

and that you want MAX_SEQ_NO in this clause instead of G.KG_TK_NO

N.KN_SEQ_NO = G.KG_TK_NO

By the way, your column names are a little difficult to work with. I don't find the KG_ prefix style useful although it is popular. But the rest of the names are too short.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文