场景: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也可以使用)
本人正在打造技术交换群,欢迎志同道合的朋侪一起探究,一起努力,通过本身的努力,在技术岗位这条道路上走的更远。QQ群号:914683950 备注:技术交换 即可通过!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |