[英]Select MAX Value for Each ROW - Oracle Sql
I have one doubt. 我有一个疑问。 I need to find what is the latest occurrence for a specific list of Customers, let's say to simplify, I need it for 3 Customers out of 100. I need to check when it was the last time each of them got a bonus.
我需要查找特定客户列表的最新发生情况,为了简化起见,我需要100个客户中的3个。我需要检查这是何时每个客户最后一次获得奖金。 The table would be:
该表将是:
EVENT_TBL
Fields: Account ID
, EVENT_DATE
, BONUS ID
, .... 字段:
Account ID
, EVENT_DATE
, BONUS ID
,...。
Can you suggest a way to grab the latest ( MAX
) EVENT DATE
(that means one row each) 您能否提出一种获取最新(
MAX
) EVENT DATE
(即每行一行)
I'm using SELECT...IN
to specify the Account ID but not sure how to use MAX
, Group BY
etc etc (if ever needed). 我正在使用
SELECT...IN
来指定帐户ID,但不确定如何使用MAX
, Group BY
等(如果需要)。
Use the ROW_NUMBER()
analytic function: 使用
ROW_NUMBER()
分析函数:
SELECT *
FROM (
SELECT t.*,
ROW_NUMBER() OVER ( PARTITION BY Account_id ORDER BY event_date DESC ) AS rn
FROM EVENT_TBL t
WHERE Account_ID IN ( 123, 456, 789 )
)
WHERE rn = 1
You can try 你可以试试
with AccountID_Max_EVENT_DATE as (
select AccountID, max(EVENT_DATE) MAX_D
from EVENT_TBL
group by AccountID
)
SELECT E.*
FROM EVENT_TBL E
INNER JOIN AccountID_Max_EVENT_DATE M
ON (E.AccountID = M.AccountID AND M.MAX_D = E.EVENT_DATE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.