‘壹’ sql字段累加求和的问题
SELECT ID,STRING1,STRING2 from table UNION
select ID=0,string1=string1+'小计',countresult=count(1),sum1=sum(string2) from talbe group by string1 union
select ID=0,string1='总计',countresult=count(1),sum1=sum(string2) from table order by string1,id
结果为包括了小计和总计,和明细,并按顺序排列
‘贰’ SQL语句如何实现数据的累加
假设原来的表是t1:
declare @t table ([date] int,sale int,[sum] int)
declare @dt int,@sale int,@sum int
declare @c cursor
set @sum=0
set @c=cursor forward_only read_only for select * from t1 order by dt
open @c
fetch next from @c into @dt,@sale
while @@fetch_status=0
begin
set @sum=@sum+@sale
insert @t values (@dt,@sale,@sum)
fetch next from @c into @dt,@sale
end
close @c
deallocate @c
select * from @t
这是用存储过程实现,至于如何用一条语句实现,我想不出来。
‘叁’ sql怎么把一行数据中的几列相加
1、创建测试表,create table test_num(fee_id number, fee1 number, fee2 number, fee_3 number);
‘肆’ 怎样把用sql语句把数据库中查询出来的每一条结果加起来
select
count(*) , -- 查询记录条数
sum(累加的列) -- 查询结果累加
from accuont
where
VIP = 1
‘伍’ sql 如果把一列所有的行的值叠加
如果是oracle数据库的话,可以尝试如下思路,否则,可能只能自定义函数实现
with t1 as (
select 1 f1,'a' f2 from al
union all
select 2 f1,'b' f2 from al
union all
select 3 f1,'c' f2 from al
union all
select 4 f1,'d' f2 from al
)
select f1,max(substr(sys_connect_by_path(f4,','),2)) f2
from (
select t1.f1,t1.f2,t2.f1 f3,t2.f2 f4
from t1, t1 t2
) t
connect by prior f1 = f1 and prior f3 = f3 + 1
group by f1
‘陆’ SQL中计算字段的累加和的函数是统计项目数的函数是有谁知道吗
1、首先新建一个test数据库,在数据库里新建一张type表,里面插入三条测试数据。
‘柒’ SQL 的一个累加算法
select t2.id,t2.a,sum(t1.a)
from t_a t1 left join t_a t2 on t1.id<=t2.id
group by t2.id,t2.a
是这个意思吗?
表名我起的叫t_a,应该和你一样,字段名也引用你的,直接运行看看结果
-----------补充----------
这个就可以显示你要求的结果啊,你是要更新b列的数据吗?你数据库是什么?
更新b列用这个
update t_a set t_a.b=s.sum_value from t_a
inner join (select t2.id,t2.a,sum(t1.a) sum_value from t_a t1 left join t_a t2 on t1.id<=t2.id
group by t2.id,t2.a) s on t_a.id=s.id and t_a.a=s.a
sqlserver下的写法,其他数据库写法可能略有出入,有问题帮你调
‘捌’ SQL如何实现:同列数据逐行相加
给一个DB2的
SELECT t2.A, SUM(t1.A)
FROM (SELECT a, ROW_NUMBER() OVER(ORDER BY A) RK from tab) t1, (SELECT a, ROW_NUMBER() OVER(ORDER BY A) RK from tab) t2
WHERE t1.rk <= t2.rk
group by t2.a
ORDER BY T2.A
-------------------------
....+....1....+....2....+....3....+....4....+....5
A SUM ( T1 . A )
11.25 11.25
25.25 36.50
50.00 86.50
60.00 146.50
ORACLE最好办
SELECT t2.A, SUM(t1.A)
FROM (SELECT a, rownum RK from tab) t1
(SELECT a, rownum RK from tab) t2
WHERE t1.rk <= t2.rk
group by t2.a
ORDER BY T2.A
关键是求得行号
如果你所有的数据都是唯一的,并且是递增排列了,那也有另外一个办法的
SELECT t2.a, sum(t1.a)
FROM TAB t1, tab t2
WHERE t1.a <= t2.a
group by t2.a
‘玖’ sql 每一行的字段求和
SELECT列1+列2+列3……+列NASTotal
FROM表
把你想要计算的列都加进去,就OK了
‘拾’ 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;