SQL 流水賬餘額查詢

2022-05-13 05:12:27 字數 3110 閱讀 9578

建立表

create

table

[dbo

].[test](

[id][

int]

identity(1,1) not

null

, [rq

][date

]null

, [sr

][int]

null

, [zc

][int]

null

) on

[primary

]

插入資料

insert

into

[dbo

].[test

]([rq

],[sr

],[zc])

values('

2017-10-01 00:00:00

',30,null

);insert

into

[dbo

].[test

]([rq

],[sr

],[zc])

values('

2017-10-02 00:00:00

',null,20

);insert

into

[dbo

].[test

]([rq

],[sr

],[zc])

values('

2017-10-03 00:00:00

',20,null

);insert

into

[dbo

].[test

]([rq

],[sr

],[zc])

values('

2017-10-04 00:00:00

',null,10

);insert

into

[dbo

].[test

]([rq

],[sr

],[zc])

values('

2017-10-06 00:00:00

',40,null);

日期欄位不重複

declare

@stardate

date

declare

@enddate

date

set@stardate='

2017-10-02

'set

@enddate='

2017-10-07

'select'期初

' as

日期, isnull(sum(sr), '

-') as 收入, isnull(sum(zc), '

-') as 支出, isnull(sum(sr) -

sum(zc), '

-') as

餘額from

test

where rq <

@stardate

union

allselect

cast(t.rq as

nvarchar), isnull(t.sr, 0), isnull(t.zc, 0), isnull

( (

select

sum(t2.sr) -

sum(t2.zc)

from test as

t2

where t2.rq <=

t.rq

),0)

from test as

twhere t.rq >=

@stardate

and t.rq <=

@enddate

union

allselect'合計

日期不唯一, 用自增字段來區分:

declare

@stardate

date

declare

@enddate

date

set@stardate='

2017-10-02

'set

@enddate='

2017-10-07

'select

cast('期初'

asvarchar) as 序號,''

as 日期, isnull(sum(sr), '

-') as 收入, isnull(sum(zc), '

-') as 支出, isnull(sum(sr) -

sum(zc), '

-') as

餘額from

test

where rq <

@stardate

union

allselect

cast(t.id as

varchar),cast(t.rq as

nvarchar), isnull(t.sr, 0), isnull(t.zc, 0), isnull

( (

select

sum(t2.sr) -

sum(t2.zc)

from test as

t2

where t2.id <=

t.id

),0)

from test as

twhere t.rq >=

@stardate

and t.rq <=

@enddate

union

allselect'合計

月季流水賬

最近比較忙,好久沒有博了,先花幾天把我部落格周圍的荒草拔掉先.然後嘮叨嘮叨最近兩個月來印象比較深的值得一記的東西和想法。0.先詳細地談談工作 3 damn f u c k why程式除錯not通過?此處省去5千萬字 總之,這是有成效的乙個月,成功的乙個月,勝利的乙個月,在 的指引下,我們的工作取得了...

月季流水賬

最近比較忙,好久沒有博了,先花幾天把我部落格周圍的荒草拔掉先.然後嘮叨嘮叨最近兩個月來印象比較深的值得一記的東西和想法。0.先詳細地談談工作 3 damn f u c k why程式除錯not通過?此處省去5千萬字 總之,這是有成效的乙個月,成功的乙個月,勝利的乙個月,在 的指引下,我們的工作取得了...

教務流水賬

到今天為止教務暫時告一段落,基本功能是實現了,但是不完美的地方還有很多,比如計算工作量的時候依然不能完全的取代每學年的工作量計算的工作,校歷錄入還是很繁瑣,課程安排不支援 excel 匯入,排課程表的時候依然不是非常的人性化等等,還有很多需要改進的地方。但是不管怎麼說我們的基本功能是做完了,先複習軟...