当前位置:首页 » 编程语言 » 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')