[英]Oracle SQL - Only return value for enquiry ID with the highest enquiry_log_number
[英]Oracle SQL - How to return the name with the highest ID ending in a certain number
我有一个这样的表格,我需要在其中获取 ID 的最后一个数字,有多少人的 ID 以该数字结尾,以及 ID 最高的人:
Members: |ID |Name |
-----------------
|123 |foo |
|456 |bar |
|789 |boo |
|1226|far |
我需要得到的结果看起来像这样
|LAST_NUMBER |OCCURENCES |HIGHEST_ID_GUY |
---------------------------------------------
|3 |1 |foo |
|6 |2 |far |
|9 |1 |boo |
但是,虽然我可以正确显示前两个结果,但我不知道如何显示 HIGHEST_ID_GUY。 我的代码如下所示:
SELECT DISTINCT SUBSTR(id, LENGTH(id - 1), LENGTH(id)) AS LAST_NUMBER,
COUNT(*) AS OCCURENCES
/* This is where I need to add HIGHEST_ID_GUY */
FROM Members
GROUP BY SUBSTR(id, LENGTH(id - 1), LENGTH(id))
ORDER BY LAST_NUMBER
任何帮助表示赞赏:)
如果id
是数字,则使用算术运算:
select mod(id, 10) as last_digit,
count(*),
max(name) keep (dense_rank first order by id desc) as name_at_biggest
from t
group by mod(id, 10);
如果id
是一个字符串,那么您需要转换为一个数字或类似的东西来定义“最高 id”。 例如:
select substr(id, -1) as last_digit,
count(*),
max(name) keep (dense_rank first order by to_number(id) desc) as name_at_biggest
from t
group by substr(id, -1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.