oracle 分组排序取第一条

解释:Oracle 对特性字段先进行分组,对每组数据进行排序,之后取每一组中的第一条数据

所用到的函数是 ROW_NUMBER() OVER (PARTITION BY t.BEGINYEARMONTH,t.ORGID ORDER BY tc.YEARMONTH) RN

SELECT ORGID,nvl(AMOUNT/10000,0)AMOUNT FROM (SELECT
	t.ORGID,tc.AMOUNT,ROW_NUMBER() OVER (PARTITION BY t.BEGINYEARMONTH,t.ORGID ORDER BY tc.YEARMONTH) RN
FROM
	T_CASH_B_MONTHLY_ROLLING t,
	T_CASH_B_MONTHLY_ROLLING_RST tc
WHERE
	t.APPROVESTATE = '2'
	AND t.CANCELSTATE = '1'
	AND tc.HEADERID = t.URID
	AND tc.PAYMENTMETHOD = '2'
	AND to_char(t.BEGINYEARMONTH,'yyyy-MM') >= '${begindate}'
	AND to_char(t.BEGINYEARMONTH,'yyyy-MM') <= '${enddate}') WHERE RN=1

你可能感兴趣的:(笔记,Oracle,oracle)