『壹』 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;