⑴ 怎么用sql算出两个表中数量的百分比
with t1 as (select count(*) f from a), t2 as (select count(*) f from b)
select (select f*1. from a)/((select f from a)+(select f from b))
⑵ SQL 存储过程求和,求同比,请高手解答!
以A表为例子
先建立一个table0
create table_0
(n_date date,
sheng varchar2(20),
sale number
tongbi number);
create unique index table_0_U1 on table_0 (n_date,sheng);
create or replace package body pkg_b_tongji is
procere sp_update_party_rating(p_sdate number, p_edate number) is
v_sdate date default to_date(p_sdate,'yyyymmdd');
v_edate date default to_date(p_edate,'yyyymmdd');
v_sqlUpd varchar2(3000);
v_sqlIns varchar2(3000);
begin
v_sqlIns := 'insert into table_0(n_date,sheng,sale)
values(:v1,:v2,:v3)';
v_sqlUpd := 'update table_0 t set sale = :v1
where n_date = :v2 and sheng = :v3';
for c1 in (select a.ndate,
a.sheng,
sum(sale) sale
from a
where ndate between v_sdate and v_edate
group by a.ndate,
a.sheng
)
loop
execute immediate v_sqlUpd using c1.sale;
if sql%rowcount<=0 then --如果更新操作没有执行就执行插入操作
execute immediate v_sqlIns using c1.n_date,c1.sheng,c1.sale;
end if;
end loop;
commit;
end sp_update_party_rating;
---更新同比
procere sp_update_tongbi is
begin
for c2 in (
select n_date,
sheng,
sale,
nvl(sale,0) sale1
from table_0 a
left join
(select n_date,sheng,a.nvl(sale,0) sale
from table_0 a,
(select t.n_date,sheng
add_months(n_date,-1) n_date2
from table_0 t)
where a.sheng = b.sheng and a.n_date = b.n_date2)
)
loop
update table_0
set tongbi = sale/sale1
where n_date = c2.n_date and sheng = c2.sheng;
commit;
end loop;
end sp_update_tongbi;
end pkg_b_tongji;
⑶ 如何用SQL计算同比
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
⑷ 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
⑸ SQL 求两个字段的百分比 怎么写
结果如下:
round((SA/TotelTime)*100,2) & "%"
select mz as '民族',count(*) as '人数',SUBSTRING (convert(varchar (20),
(count(*)/80.00*100) ),1,4)+'%' as '比例' from ryxx group by mz
⑹ 某一数据的7天的同比均值用SQL怎么实现
某一数据的7天的同比均值用SQL怎么实现
在空白处,选择一个空格,输入“=AVERAGE()” 然后选择7个产品价格数据,就会自动算出平均数 其他的类似操作,还是比较快的
⑺ sql中求百分比函数
若悉迹针对每行求百分比:橘陆尺
select SA/TotelTime ,SB/TotelTime ,SC/TotelTime ,SD/TotelTime ,SE/TotelTime from 表名;
若是对总计后的值求百分比:
select sum(SA)/sum(TotelTime) ,sum(SB)/sum(TotelTime) ,sum(SC)/sum(TotelTime) ,sum(SD)/sum(TotelTime) ,sum(SE)/sum(TotelTime) from 表名;
当圆高然,以上都是以小数形式显示结果,若要以百分比形式显示结果:乘以100,并保留两位小数,然后加上“%”即可。
⑻ 生手求教oracle同比和环比sql语句
substr(t.salarymonth, -2)) "同比",
sum(t.salary) /
(select sum(t1.salary)
from D_MONTH_SALARY t1
where t1.salarymonth =
to_char(to_date(t.salarymonth, 'yyyymm') - 1, 'yyyymm')) "环比"
from D_MONTH_SALARY t
group by t.salarymonth;
⑼ 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 name,str((case (select isnull(sum(cost),0) from table1) when 0 then 0 else cost/(select isnull(sum(cost),0) from table1) end)*100,10,2)+'%' as bfb from table1
以上sql语句较长,解释一下:
使用case...when...end,是为了避免合计cost为0的话,出现无法除0的错误;结果值乘以100是为了配合百分比格式;使用str(...,10,2)函数是把计算出来的数字值转换成两位小数精度的文本串;最后,在结果后加上一个百分号,效果就完整了。
以上语句在sqlserver数据库中测试通过。