场景:oracle在展示数据时,用户需要看到当前记录数,添加序号即可,适用于表格导出。
方式一:添加xh字段
select fxh, fmc, round( ( sum( fczsrbnljzxs ) - sum( fczsrsntqs ) ) / 10000, 2 ) as 增减额 , round( sum( fczsrbnljzxs ) / 10000, 2 ) as 财政收入 , round( sum( fczsrbsn ), 2 ) as 增减幅度 , f_sys_year from zhzs_bill_qqczsrfjwcqk where f_sys_year = '2021' and f_sys_month = '12' group by fxh, fmc, f_sys_year order by to_number( fxh )
效果图:
sql解读:此条在select后添加xh,group by后添加xh分组,以及用order by排序,to_number(xh)将其转为数值型即可
方式二:oracle自带
select rownum as fxh,a.* from ( select fmc , round((sum(fczsrbnljzxs) - sum(fczsrsntqs)) / 10000, 2) as 增减额 , round(sum(fczsrbnljzxs) / 10000, 2) as 财政收入 , round(sum(fczsrbsn), 2) as 增减幅度 , f_sys_year from zhzs_bill_qqczsrfjwcqk where f_sys_year = '2021' and f_sys_month = '12' group by fmc, f_sys_year ) a
效果图
sql解读:此方法使用自带的rownum函数,简单方便,oracle独有
方式三:row_number()函数
select row_number() over(order by fmc) as xh,fmc , round((sum(fczsrbnljzxs) - sum(fczsrsntqs)) / 10000, 2) as 增减额 , round(sum(fczsrbnljzxs) / 10000, 2) as 财政收入 , round(sum(fczsrbsn), 2) as 增减幅度 , f_sys_year from zhzs_bill_qqczsrfjwcqk where f_sys_year = '2021' and f_sys_month = '12' group by fmc, f_sys_year
效果图
sql解读:此方法通用(mysql也可以使用)
附:oracle使用row_number()函数查询时增加序号列
使用oracle自带的row_number()函数能够实现自动增加序号列的要求,但是同时引发一个问题,如果我们查询出来的数据需要使用order by排序的话,那么我们会发现新增加的序号列是乱序的,它会根据我们order by后面的字段重新排序,那么怎么解决这一问题呢。
很简单,我们再加上根据order by排序就可以了。
select row_number() over(order by t.taskcreatetime), t.activityname, t.tasktype from t_sys_flow_task t order by t.taskcreatetime;
查询结果如下:
总结
到此这篇关于oracle添加序号列3种方法的文章就介绍到这了,更多相关oracle添加序号列内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论