跟日期有關的兩條經典SQL語句

2021-03-31 08:56:58 字數 4377 閱讀 6295

1.用一條語句得出某日期所在月份的最大天數?

select day(dateadd(dd, -day('2004-02-13'), dateadd(mm, 1, '2004-02-13'))) as 'day number'

2.少記錄變成多條記錄問題

有表tbl

日期       收入    支出

2004-02-11 00:00:00 60 45

2004-03-01 00:00:00 60 45

2004-03-02 00:00:00 40 50

2004-03-05 00:00:00 50 40

/*測試資料:

create table tbl([日期] **alldatetime,[收入] int ,[支出] int) insert into tbl

select '2004-02-11', 60, 45

union select '2004-03-01',60, 45

union select '2004-03-02',40, 50

union select '2004-03-05',50, 40

*/要得到的結果:

日期       收入    支出     餘額

2004-02-01 00:00:00 null null null

2004-02-02 00:00:00 null null null

2004-02-03 00:00:00 null null null

2004-02-04 00:00:00 null null null

2004-02-05 00:00:00 null null null

2004-02-06 00:00:00 null null null

2004-02-07 00:00:00 null null null

2004-02-08 00:00:00 null null null

2004-02-09 00:00:00 null null null

2004-02-10 00:00:00 null null null

2004-02-11 00:00:00 60 45 15

2004-02-12 00:00:00 null null 15

2004-02-13 00:00:00 null null 15

2004-02-14 00:00:00 null null 15

2004-02-15 00:00:00 null null 15

2004-02-16 00:00:00 null null 15

2004-02-17 00:00:00 null null 15

2004-02-18 00:00:00 null null 15

2004-02-19 00:00:00 null null 15

2004-02-20 00:00:00 null null 15

2004-02-21 00:00:00 null null 15

2004-02-22 00:00:00 null null 15

2004-02-23 00:00:00 null null 15

2004-02-24 00:00:00 null null 15

2004-02-25 00:00:00 null null 15

2004-02-26 00:00:00 null null 15

2004-02-27 00:00:00 null null 15

2004-02-28 00:00:00 null null 15

2004-02-29 00:00:00 null null 15

2004-03-01 00:00:00 60 45 30

2004-03-02 00:00:00 40 50 20

2004-03-03 00:00:00 null null 20

2004-03-04 00:00:00 null null 20

2004-03-05 00:00:00 50 40 30

2004-03-06 00:00:00 null null 30

2004-03-07 00:00:00 null null 30

2004-03-08 00:00:00 null null 30

2004-03-09 00:00:00 null null 30

2004-03-10 00:00:00 null null 30

2004-03-11 00:00:00 null null 30

2004-03-12 00:00:00 null null 30

2004-03-13 00:00:00 null null 30

2004-03-14 00:00:00 null null 30

2004-03-15 00:00:00 null null 30

2004-03-16 00:00:00 null null 30

2004-03-17 00:00:00 null null 30

2004-03-18 00:00:00 null null 30

2004-03-19 00:00:00 null null 30

2004-03-20 00:00:00 null null 30

2004-03-21 00:00:00 null null 30

2004-03-22 00:00:00 null null 30

2004-03-23 00:00:00 null null 30

2004-03-24 00:00:00 null null 30

2004-03-25 00:00:00 null null 30

2004-03-26 00:00:00 null null 30

2004-03-27 00:00:00 null null 30

2004-03-28 00:00:00 null null 30

2004-03-29 00:00:00 null null 30

2004-03-30 00:00:00 null null 30

2004-03-31 00:00:00 null null 30

答案:select y.[日期], tbl.[收入], tbl.[支出], (

select sum(isnull(tbl.[收入], 0)-isnull(tbl.[支出], 0)) from tbl where [日期]<=y.[日期]) as [餘額]

from tbl right join (

select dateadd(dd, n.i, dateadd(dd, 1-day(m.minday), m.minday)) as [日期]

from (

select 0 as i

union all select 1

union all select 2

union all select 3

union all select 4

union all select 5

union all select 6

union all select 7

union all select 8

union all select 9

union all select 10

union all select 11

union all select 12

union all select 13

union all select 14

union all select 15

union all select 16

union all select 17

union all select 18

union all select 19

union all select 20

union all select 21

union all select 22

union all select 23

union all select 24

union all select 25

union all select 26

union all select 27

union all select 28

union all select 29

union all select 30

union all select 31

) n,

(select min(日期) as minday

from tbl

group by datediff(month, 0, 日期)

) m

where datediff(mm, dateadd(dd, n.i, dateadd(dd, 1-day(m.minday), m.minday)), m.minday)=0) as y

on tbl.[日期]=y.日期

跟日期有關的兩條經典SQL語句

1.用一條 語句得出某日期所在月份的最大天數?select day dateadd dd,day 2004 02 13 dateadd mm,1,2004 02 13 as day number 2.少記錄變成多條記錄問題 有表tbl 日期 收入 支出 2004 02 11 00 00 00 60 ...

跟日期有關的兩條經典SQL語句

1.用一條語句得出某日期所在月份的最大天數?select day dateadd dd,day 2004 02 13 dateadd mm,1,2004 02 13 as day number 2.少記錄變成多條記錄問題 有表tbl 日期 收入 支出 2004 02 11 00 00 00 60 4...

兩條乙個的sql

public string isalarm string userid throws userexception return result public string maprow resultset rs,int num throws sqlexception return result use...