⑴ 怎麼用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資料庫中測試通過。