表结构:
id number,--编号
in_date date,--日期
in_fee number,--收入
out_fee number--支出
数据:
1,2001-03-02,30,0
2,2001-03-03,50,0
3,2001-03-04,0,20
4,2001-03-05,10,0
5,2001-03-05,15,0
现在想求每天的余额,希望结果:
1,2001-03-02,30,0,30
2,2001-03-03,50,0,80
3,2001-03-04,0,20,60
4,2001-03-05,10,0,70
5,2001-03-05,15,0,85
请问如何写sql?
id number,--编号
in_date date,--日期
in_fee number,--收入
out_fee number--支出
数据:
1,2001-03-02,30,0
2,2001-03-03,50,0
3,2001-03-04,0,20
4,2001-03-05,10,0
5,2001-03-05,15,0
现在想求每天的余额,希望结果:
1,2001-03-02,30,0,30
2,2001-03-03,50,0,80
3,2001-03-04,0,20,60
4,2001-03-05,10,0,70
5,2001-03-05,15,0,85
请问如何写sql?
SQL> select tt.id,
2 tt.in_date,
3 tt.in_fee,
4 tt.out_fee,
5 sum(in_fee-out_fee) over(order by tt.id,tt.in_date) as results
6 from tablename tt; ID IN_DATE IN_FEE OUT_FEE RESULTS
---------- ----------- ---------- ---------- ----------
1 3/2/2001 30 0 30
2 3/3/2001 50 0 80
3 3/4/2001 0 20 60
4 3/5/2001 10 0 70
5 3/5/2001 15 0 85
t.*,
(select sum(in_fee-out_fee) from 表 where id<=t.id) as 日结
from
表 t
4,2001-03-02,30,0
3,2001-03-03,50,0
1,2001-03-04,0,20
2,2001-03-05,10,0
5,2001-03-05,15,0
这样如果按“子陌红尘”您的SQL得到结果:
SQL> select t.*,(select sum(in_fee-out_fee) from test where v_id<=t.v_id) as 日结 from test t
2 order by in_date; V_ID IN_DATE IN_FEE OUT_FEE 日结
---------- ------------------- ---------- ---------- ----------
4 2001-03-02 00:00:00 30 0 70
3 2001-03-03 00:00:00 50 0 40
1 2001-03-04 00:00:00 0 20 -20
2 2001-03-05 00:00:00 10 0 -10
5 2001-03-05 00:00:00 15 0 85
这样的结果是错的!
还有没有什么办法能查询呢?
好像用分析函数rownum是可以解决的 ...SQL> select t1.id,
2 t1.in_date,
3 t1.in_fee,
4 t1.out_fee,
5 sum(in_fee-out_fee) over(order by rownum,t1.in_date) as results
6 from tablename t1; ID IN_DATE IN_FEE OUT_FEE RESULTS
---------- ----------- ---------- ---------- ----------
3 3/2/2001 30 0 30
4 3/3/2001 50 0 80
2 3/4/2001 0 20 60
1 3/5/2001 10 0 70
5 3/5/2001 15 0 85
请问枫の叶,用标准SQL,不用分析函数,能实现这个查询吗?
现有如下结构的表:
编号----产品名称----产量(int)----日期(varchar)
1 ----某产品1 ----100 ----20060922
2 ----某产品1 ----130 ----20060923
3 ----某产品2 ----104 ----20060924
5 ----某产品1 ----100 ----20060925
7 ----某产品1 ----200 ----20060926
.
.
.
现在要求查询每天的累计产量如:
20060923的累计产量就为20060922的100+20060923的130=230.
20060924的累计产量就为230+104=334,
20060925的累计产量为334+100=434...
请问该如何查询?
create table #a(
[id] [char] (10),
[proname] [char] (10),
[output] [int],
[dt] [datetime]
)insert into #a(id,proname,output,dt) values('1','某产品1',100,'2006-09-22')
insert into #a(id,proname,output,dt) values('2','某产品1',130,'2006-09-23')
insert into #a(id,proname,output,dt) values('3','某产品2',104,'2006-09-24')
insert into #a(id,proname,output,dt) values('5','某产品1',100,'2006-09-25')
insert into #a(id,proname,output,dt) values('7','某产品1',200,'2006-09-26')select *,(select sum(output) from #a where dt<=b.dt) as '每日总量'
from #a b
order by dtdrop table #a
id proname output dt 每日总量
---------- ---------- ----------- ------------------------------------------------------ -----------
1 某产品1 100 2006-09-22 00:00:00.000 100
2 某产品1 130 2006-09-23 00:00:00.000 230
3 某产品2 104 2006-09-24 00:00:00.000 334
5 某产品1 100 2006-09-25 00:00:00.000 434
7 某产品1 200 2006-09-26 00:00:00.000 634(所影响的行数为 5 行)
if object_id('pubs..tb') is not null
drop table tb
gocreate table tb(日期 varchar(10),品种 varchar(10),产量 int)
insert into tb(日期,品种,产量) values('2007-04-04', 'JC10' , 20)
insert into tb(日期,品种,产量) values('2007-04-04', 'T/C20', 30)
insert into tb(日期,品种,产量) values('2007-04-05', 'T/C20', 40)
insert into tb(日期,品种,产量) values('2007-04-05', 'JC10' , 10)
insert into tb(日期,品种,产量) values('2007-04-06', 'JC10' , 15)
goselect *,(select sum(产量) from tb where 日期<=b.日期 and 品种 = b.品种) as '本月累计' from tb bdrop table tb日期 品种 产量 本月累计
---------- ---------- ----------- -----------
2007-04-04 JC10 20 20
2007-04-04 T/C20 30 30
2007-04-05 T/C20 40 70
2007-04-05 JC10 10 30
2007-04-06 JC10 15 45(所影响的行数为 5 行)
2006.10.01 收工资 500
2006.10.02 付房租 200
2006.10.03 购物 50目标说明: 要用一条SQL语句生成结果集如下(比上表多一个结余)
每条记录的 结 = 上一条记录的结余+当前记录收入-当前记录支出
日期(RQ),...项目(XM),收入, 支出
2006.10.01 收工资 500
2006.10.02 付房租 200
2006.10.03 购物 50declare @t table (
收支日期RQ datetime,
收支项目XM varchar(20),
收入金额SL int,
支出金额ZC int
)
insert @t
select '2006.10.01','收工资',500,0 union all
select '2006.10.02','付房租',0,200 union all
select '2006.10.03','购物支出',0,50
select 收支日期RQ , 收支项目XM , 收入金额SL , 支出金额ZC , 结余JY = isnull(收入金额SL - 支出金额ZC ,0) into #temp from @tupdate #temp
set 结余JY=(select isnull(sum(收入金额SL - 支出金额ZC),0)
from #temp tt
where TT.收支日期RQ<=#temp.收支日期RQ)
select convert(varchar(10),收支日期RQ,120) as 收支日期RQ , 收支项目XM , 收入金额SL , 支出金额ZC , 结余JY from #tempdrop table #temp
--结果
收支日期RQ 收支项目XM 收入金额SL 支出金额ZC 结余JY
---------- ---------- ---------- ---------- -----------
2006-10-01 收工资 500 0 500
2006-10-02 付房租 0 200 300
2006-10-03 购物支出 0 50 250(所影响的行数为 3 行)
create table dbo.cost
(
id int identity(1,1) not null--编号
,in_date datetime not null--日期
,in_fee float not null--收入
,out_fee float not null--支出
,constraint PK_cost primary key
(
id asc
)
)--插入数据
insert into dbo.cost(in_date,in_fee,out_fee)
select'2001-03-02',30,0
union all
select'2001-03-03',50,0
union all
select'2001-03-04',0,20
union all
select'2001-03-05',10,0
union all
select'2001-03-05',15,0--查询
select
a.id
,a.in_date
,a.in_fee
,a.out_fee,
balance = ISNULL(a.in_fee - a.out_fee + (
select top 1 sum(in_fee - out_fee)
from dbo.cost a with(nolock)
where id < a.id),in_fee - out_fee)
from dbo.cost a with(nolock)
你的第一种查询要求没有重叠的日期,这个和我需求不和。
第二种查询有重叠日期,但是又必须按产品进行了分类求和,我的需求不用分类。
第三种查询是SQL-SERVER的语法吧,好像用到了一个临时字段?表?我是在VFP下用这个查询,这种语法不支持吧cherish的方法好,但是也不能在VFP下用啊。请问还有没有高手能写出能在VFP下运行的这个查询SQL啊?
SELECT id,in_date,in_fee,out_fee,(SELECT SUM(in_fee-out_fee) FROM your_table WHERE ROWNUM<=a.n)
FROM
(SELECT b.*,ROWNUM n FROM your_table b ) a谢谢各位。
IN_FEE, OUT_FEE,
IN_FEE - OUT_FEE +
(SELECT ISNULL(SUM(IN_FEE)- SUM(OUT_FEE),0) FROM TEST WHERE ID <> A.ID AND IN_DATE < A.IN_DATE)
FROM TEST A
ORDER BY IN_DATE