sql同比
㈠ 請教Oracle計算同比和環比sql語句
首先明確概念:
環比增長率=(本期數-上期數)/上期數*100%反映本期比上期增長了多少。
同比增長率=(本期數-同期數)/同期數*100%指和去年同期相比較的增長率。
數據表名:d_temp_data
查詢數據如下:
查詢的sql語句如下:
selecta.*,
nvl(round(money/lag(money)over(orderbyid)*100,2),'0')||'%'"同比",
nvl(round(money/lag(money)over(partitionbymonorderbyid)*100,2),'0')||'%'"環比"
fromd_temp_dataa
idinameyearmonmoney同比環比
----------------------------------------------------------------------
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%
㈡ SQL SEVER求和算同比
select月份,(SUM(casewhen年份=2015then出口量else0end)-SUM(casewhen年份=2014then出口量else0end))*100.0/SUM(casewhen年份=2014then出口量else0end)增長率百分比
from[hgsj].[dbo].[seamless]
where年份in(2014,2015)
groupby月份
㈢ SQL里怎麼查詢銷售同比與環比
與上月比即為環比,與上年同月比即為同比
select sum(case when to_char(fdbs,'yyyy')-1 || to_char(fdbs,'MM『) =年月參數 then sshje else 0 end ) 上年同期,sum(case when to_char(fdbs,'yyyy') || to_char(fdbs,'MM『)-1 = 年月參數 then sshje else 0 end ) 上月銷售額,sum(case when to_char(fdbs,'yyyy') || to_char(fdbs,'MM') = 年月參數 then sshje else 0 end ) 本月銷售額
from retmls