请教Oracle计算同比和环比sql语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请教Oracle计算同比和环比sql语句相关的知识,希望对你有一定的参考价值。
首先明确概念:
环比增长率=(本期数-上期数)/上期数*100% 反映本期比上期增长了多少。
同比增长率=(本期数-同期数)/同期数*100% 指和去年同期相比较的增长率。
数据表名:d_temp_data
查询数据如下:
查询的sql语句如下:
select a.*,
nvl(round(money/lag(money) over(order by id)*100,2),\'0\')||\'%\' "同比",
nvl(round(money/lag(money) over(partition by mon order by id)*100,2),\'0\')||\'%\' "环比"
from d_temp_data a
id iname year mon money 同比 环比
----------------------------------------------------------------------
1 1 飞马股 2013 01 300 0% 0%
2 2 飞马股 2013 02 270 90% 0%
3 3 飞马股 2013 03 350 129.63% 0%
4 4 飞马股 2013 04 180 51.43% 0%
5 5 飞马股 2013 05 500 277.78% 0%
6 6 飞马股 2013 06 400 80% 0%
7 7 飞马股 2014 01 210 52.5% 70%
8 8 飞马股 2014 02 240 114.29% 88.89%
9 9 飞马股 2014 03 320 133.33% 91.43%
10 10 飞马股 2014 04 480 150% 266.67%
11 11 飞马股 2014 05 400 83.33% 80%
参考技术A select (t.user-(select sum(t1.user) from mytable t1 where t1.time='2015/1/3' and t1.area=t.area))/-(select sum(t1.user) from mytable t1 where t1.time='2015/1/3' and t1.area=t.area) from mytable t where t.time='2015/1/4';注意这边如果time是时间类型需要对条件进行转化比如time=to_date('2015/1/4','yyyy/mm/dd')这样本回答被提问者和网友采纳 参考技术B 1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 mysql)
MySQL统计同比环比SQL
MySQL没有类似oracle方便的统计函数,只能靠自己去硬计算:通过时间字段直接增加年份、月份,然后通过left join关联时间字段去计算环比、同比公式即可
原始表结构:
求同比SQL:
- -- 按年同比
- SELECT t5.*, CASE WHEN t5.last_energy_year IS NULL OR t5.last_energy_year =0 THEN 0.00
- ELSE FORMAT(((t5.energy_year - t5.last_energy_year)/t5.last_energy_year)*100,2)
- END YoY -- 同比
- FROM (
- SELECT t3.*,t4.last_energy_year
- FROM
- (
- SELECT DATE_FORMAT(CONCAT(t1.`tyear`, ‘-‘, t1.`tmonth`,‘-01‘),‘%Y-%m-%d‘) AS YearMonth,t1.tyear,SUM(t1.energy_month) energy_year,t1.linename
- FROM(SELECT
- t.*,COUNT(DISTINCT t.`tyear`,t.`tmonth`,t.`linename`) c_cot
- FROM `ksh_tgyjy_llxgyjydlxx` t
- GROUP BY t.`linename`,t.`tyear`,t.`tmonth`,t.`linename`) t1
- GROUP BY t1.tyear,t1.`linename`
- ) t3
- LEFT JOIN
- (
- SELECT DATE_ADD(DATE_FORMAT(CONCAT(t1.`tyear`, ‘-‘, t1.`tmonth`,‘-01‘),‘%Y-%m-%d‘),INTERVAL 1 YEAR) AS lastYearMonth,t1.tyear,SUM(t1.energy_month) last_energy_year,t1.linename
- FROM(SELECT t.*,COUNT(DISTINCT t.`tyear`,t.`tmonth`,t.`linename`) c_cot
- FROM `ksh_tgyjy_llxgyjydlxx` t
- GROUP BY t.`linename`,t.`tyear`,t.`tmonth`,t.`linename`) t1
- GROUP BY t1.tyear,t1.`linename`
- ) t4
- ON t3.YearMonth = t4.lastYearMonth
- AND t3.linename = t4.linename
- ) t5
查询结果:
求月环比SQL:
- -- 按月同比
- SELECT DATE_FORMAT(t5.YearMonth,‘%Y-%m‘) YearMonth,t5.`linename`,t5.`energy_month`,
- CASE WHEN t5.lat_energy_month IS NULL OR t5.lat_energy_month=0 THEN 0.00
- ELSE FORMAT(((t5.energy_month - t5.lat_energy_month)/t5.lat_energy_month)*100,2)
- END YoY
- FROM (
- SELECT t3.*,t4.energy_month lat_energy_month
- FROM
- (
- SELECT DATE_FORMAT(CONCAT(t1.`tyear`, ‘-‘, t1.`tmonth`,‘-01‘),‘%Y-%m-%d‘) AS YearMonth,t1.*
- FROM(SELECT
- t.*,COUNT(DISTINCT t.`tyear`,t.`tmonth`,t.`linename`) c_cot
- FROM `ksh_tgyjy_llxgyjydlxx` t
- GROUP BY t.`linename`,t.`tyear`,t.`tmonth`,t.`linename`) t1
- ) t3
- LEFT JOIN
- (
- SELECT DATE_ADD(DATE_FORMAT(CONCAT(t1.`tyear`, ‘-‘, t1.`tmonth`,‘-01‘),‘%Y-%m-%d‘),INTERVAL 1 YEAR) AS lastYearMonth,t1.*
- FROM(SELECT t.*,COUNT(DISTINCT t.`tyear`,t.`tmonth`,t.`linename`) c_cot
- FROM `ksh_tgyjy_llxgyjydlxx` t
- GROUP BY t.`linename`,t.`tyear`,t.`tmonth`,t.`linename`) t1
- ) t4
- ON t3.YearMonth = t4.lastYearMonth
- AND t3.linename = t4.linename
- ) t5
查询结果:
以上是关于请教Oracle计算同比和环比sql语句的主要内容,如果未能解决你的问题,请参考以下文章