當前位置:首頁 » 編程語言 » sql存儲過程跨行求和
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql存儲過程跨行求和

發布時間: 2022-09-09 00:46:51

1. sql 多行求和

sum
average
只能對數值型參數進行運算
可以用
to_number

varchar
轉換成數值型
select
sum
(
to_number
(
col_vchar
)
)
from
ur_table;

2. SQL指定行求和,求個存儲過程~

select 創建人,sum(case when 工作類型='T106' then 耗時 else 0 end) as T106,
sum(case when 工作類型='T102' then 耗時 else 0 end) as T102,
sum(case when 工作類型='T106' then 耗時 else 0 end)
+sum(case when 工作類型='T102' then 耗時 else 0 end) as 總耗時
from test where 工作類型 in ('T106','T102') or 創建人 in ('A','B')
group by 創建人;

3. SQL里邊的求和語句怎麼寫

用select sum(列名),sum(列名),sum(列名)…… from 表 where 條件。

4. 怎麼用sql存儲過程來跨行求和並且插入到資料庫中,急用,跪謝啦

跨行求和,判斷rownum(oracle)或者類似的參數的奇數偶數即可吧。

5. SQL的多行相加求和,應該怎麼寫

假設表TA
name 名稱
month 月份
sal 工資

那麼查詢所有月份所有人 工資總額
select sum(sal) from TA

查詢所有人各月的工資,即工資按月分組
select month,sum(sal) from TA group by month

查詢各人所有月份的工資總額,即按人員分組
select name,sum(sal) from TA group by name;

6. 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;

7. sql 每一行的欄位求和

SELECT列1+列2+列3……+列NASTotal
FROM表
把你想要計算的列都加進去,就OK了

8. SQL 同一個表裡的多行怎麼相加求和

你上面的SQL語句不就是么
只要把金額去掉就可以了啊

select SUM(金額),名稱,金額, aa from test1
改成
select SUM(金額),名稱,aa from test1

9. sql server 如何對一個欄位的某幾行求和

select sum(id4),count(*) from a_temp ;

可以一句SQL就直接查詢得到列的和以及記錄數。

該SQL中sum(id4)是列id4的總和,

count(*)是得到的數據總行數。

10. 一句SQL語句實現跨表查詢並求和。

select sum((book2.售價 - book1.進貨價) * book2.數量) from book2 inner join book1 where book2.書名 = '春秋演義' and (book2.銷售日期 between '2016-10-1' and '2016-10-31')