Oracle 通过连接两个表获得最大值
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想获得最大值,请使用
MAX
和GROUP BY
。但您并不是真的想要这样,您想要获得具有最大值的行。为此,您需要根据您感兴趣的列对行进行排名,然后仅使用第一个。
If you want to get the maximum value, you use
MAX
andGROUP 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.
从您的预期输出中怀疑您不需要 GROUP BY 的第四个变量。
并且您希望在此子句中使用 MAX_SEQ_NO 而不是 G.KG_TK_NO
顺便说一句,您的列名有点难以使用。我不认为 KG_ 前缀样式有用,尽管它很流行。但其余的名字都太短了。
Suspect from your expected output you do not want fourth variable of GROUP BY.
and that you want MAX_SEQ_NO in this clause instead of 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.