Sql 按照指定天數時間段查詢

2022-02-09 00:25:28 字數 2946 閱讀 2341

begin

try

declare

@begintime

datetime='

2017-02-01

'declare

@endtime

datetime='

2017-03-3 23:59:59

'declare

@showtype

int=0--

展示的型別 0=日 1=十日 2=月

declare

@cuscount

int--

成交客戶數

declare

@totldate

int=

10--

統計分組天數

select

@cuscount

=count(1) from

(

select o.memberid from dbo.[

order]o

where o.payeddatetime between

@begintime

and@endtime

group

byo.memberid

) cuscount

if(@showtype=0

)

begin

----日

select

convert(varchar(10),a.payeddatetime,23) as showday,count(1) as 成交筆數,sum(a.payed) as 成交金額,@cuscount

as 成交客戶數 from

(

select o.payed,o.payeddatetime from dbo.[

order]o

where o.payeddatetime between

@begintime

and@endtime

) as

a

group

byconvert(varchar(10),a.payeddatetime,23

) ;

endelse

if(@showtype=1

)

begin

----十日

select a.fistdate as showday,count(1) as 成交筆數,sum(a.payed) as 成交金額,@cuscount

as 成交客戶數 from

(

select o.payed,t.evdate,convert(int, evdate-

@begintime)/

@totldate gid,@begintime

+convert(int, evdate-

@begintime)/

@totldate

*@totldate fistdate from

(select

@begintime

+number evdate from master..spt_values where type='p

'and

@begintime

+number

<=

@endtime

) t

left

join dbo.[

order

] o on

convert(varchar(100), o.payeddatetime, 112)=

convert(varchar(100), t.evdate, 112

)

where o.tradestatus=

'trade_finished

'and o.paystatus=

1and o.payeddatetime is

notnull

and o.isteambuyrede !=

'rede

'and o.storeid=

0and o.payeddatetime between

@begintime

and@endtime

) as

a

group

bya.gid,a.fistdate;

endelse

if(@showtype=1

)

begin

----月

select

substring(convert(varchar(10),a.payeddatetime,23),1,7) as showday,count(1) as 成交筆數,sum(a.payed) as 成交金額,@cuscount

as 成交客戶數 from

(

select o.payed,o.payeddatetime from dbo.[

order]o

where o.payeddatetime between

@begintime

and@endtime

) as

a

group

bysubstring(convert(varchar(10),a.payeddatetime,23),1,7

)

endelse

begin

select

''end

endtry

begin

catch

end catch

最主要是這裡 

(select @begintime+number evdate from master..spt_values where type='p' and @begintime+number<=@endtime)

SQL時間段查詢

access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 select fr...

SQL時間段查詢

sql時間段查詢 access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 ...

SQL時間段查詢

sql時間段查詢 access的話 select from table where date1 2008 1 20 and date2 2007 8 30 mysql的話 select from table where date1 2008 1 20 and date2 2007 3 30 也可以 ...