场景:oracle在展示数据时,用户需要看到当前记录数,添加序号即可,适用于表格导出。
方式一:添加xh字段
SELECTfxh,fmc,round( ( sum( fczsrbnljzxs ) - sum( fczsrsntqs ) ) / 10000, 2 ) AS 增减额,round( sum( fczsrbnljzxs ) / 10000, 2 ) AS 财政收入,round( sum( fczsrbsn ), 2 ) AS 增减幅度,f_sys_year
FROMzhzs_bill_qqczsrfjwcqk
WHEREf_sys_year = '2021' AND f_sys_month = '12'
GROUP BYfxh,fmc,f_sys_year
ORDER BYto_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也可以使用)
本人正在打造技术交流群,欢迎志同道合的朋友一起探讨,一起努力,通过自己的努力,在技术岗位这条道路上走的更远。QQ群号:914683950 备注:技术交流 即可通过!