Re: [SQL ] COUNT出来值依序加总

楼主: TeemingVoid (TeemingVoid)   2014-01-10 00:25:10
※ 引述《allllllll1 (1l1l1l1l)》之铭言:
: SELECT
: YEAR A
: MONTH A
: COUNT B
: FROM X
: GROUP BY YEAR A , MONTH A
: ORDER BY YEAR A , MONTH A
: 会出现
: A A B
: 请问有办法把B字段依序加总吗
: SUM(COUNT)会出错
self join,利用 on 条件找出前列的资料,例如:
create table CumulativeLab
(
id int,
numData int,
primary key (id)
)
go
insert into CumulativeLab values (1, 100)
insert into CumulativeLab values (2, 220)
insert into CumulativeLab values (3, 303)
insert into CumulativeLab values (4, 440)
insert into CumulativeLab values (5, 555)
go
select a.id, a.numData, sum(b.numData) as CumulativeNum
from CumulativeLab a inner join CumulativeLab b on a.id >= b.id
group by a.id, a.numData
order by a.id
go

Links booklink

Contact Us: admin [ a t ] ucptt.com