sql 分组取分组的第一条数据汇总
关键sql
PARTITION BY x,y (分组字段,相当于group by)
ORDER BY z --排序字段
PARTITION BY 引导的分区子句类似于聚组函数中的group by,排序子句可看成是select语句中的order by.
ROW_NUMBER() OVER(PARTITION BY dtc.code,dtc.id ORDER BY dtc.created_time DESC) groupRowNum
select sum(balance) totalBalance
from (
SELECT to_char(dtc.balance,'999999990.00') balance,
ROW_NUMBER() OVER(PARTITION BY dtc.code,dtc.id ORDER BY dtc.created_time DESC) groupRowNum
from testAccount dtc
where dtc.code = ?
and dtc.id = ?
)
where groupRowNum = 1