对Oracle多数据进行分组排序后取每组第一条记录
Sql代码
SELECT *
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY ORDER BY DESC) LEV,
.*
FROM )
WHERE LEV = 1
ORDER BY DESC
SELECT *
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY ORDER BY DESC) LEV,
.*
FROM )
WHERE LEV = 1
ORDER BY DESC
-- 分组列
-- 排序列
-- 表名
-- 排序列
注:.* -- 一定要有表名,否则会报“缺失表达式”错误
==========================================================
分组时不仅想取每组的第一条记录,还想取每组共有几条记录
Sql代码
SELECT *
FROM (
SELECT ROW_NUMBER() OVER(PARTITION BY PK_FINANCE_ACCOUNT ORDER BY amassdate DESC) LEV,
count(0) over(partition by PK_FINANCE_ACCOUNT) cnt,
hx_amass_liquid.*
FROM
hx_amass_liquid
)
WHERE LEV = 1
ORDER BY PK_FINANCE_ACCOUNT DESC
更多Oracle相关信息见 专题页面