當天 三天 七天資料統計

2021-08-21 13:12:05 字數 4357 閱讀 9075

語句一:

with s as (

select cast(getdate() as date) as 'shift_date',1 as 'do_code','當天入庫' as 'type'

union all

select cast(getdate() as date) as 'shift_date',0 as 'do_code','當天出庫' as 'type'

union all

select cast(getdate() as date) as 'shift_date',1 as 'do_code','3天入庫' as 'type'

union all

select cast(getdate() as date) as 'shift_date',0 as 'do_code','3天出庫' as 'type'

union all

select cast(dateadd(dd,-1,getdate()) as date) as 'shift_date',1 as 'do_code','3天入庫' as 'type'

union all

select cast(dateadd(dd,-1,getdate()) as date) as 'shift_date',0 as 'do_code','3天出庫' as 'type'

union all

select cast(dateadd(dd,-2,getdate()) as date) as 'shift_date',1 as 'do_code','3天入庫' as 'type'

union all

select cast(dateadd(dd,-2,getdate()) as date) as 'shift_date',0 as 'do_code','3天出庫' as 'type'

union all

select cast(getdate() as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(getdate() as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type'

union all

select cast(dateadd(dd,-1,getdate()) as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(dateadd(dd,-1,getdate()) as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type'

union all

select cast(dateadd(dd,-2,getdate()) as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(dateadd(dd,-2,getdate()) as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type'

union all

select cast(dateadd(dd,-3,getdate()) as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(dateadd(dd,-3,getdate()) as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type'

union all

select cast(dateadd(dd,-4,getdate()) as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(dateadd(dd,-4,getdate()) as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type'

union all

select cast(dateadd(dd,-5,getdate()) as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(dateadd(dd,-5,getdate()) as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type'

union all

select cast(dateadd(dd,-6,getdate()) as date) as 'shift_date',1 as 'do_code','7天入庫' as 'type'

union all

select cast(dateadd(dd,-6,getdate()) as date) as 'shift_date',0 as 'do_code','7天出庫' as 'type')

select s.type,sum(hand_num) as 'in_out_qty'

from [zws].[dbo].[zws] a left join s on a.shift_date=s.shift_date and a.do_code=s.do_code

where a.shift_date>='2018-07-10'

group by  s.type

order by  s.type 

語句二:

select *,sum(qty) over(partition by right(shift_date,2) order by qty) as 'new_qty'

from 

(select case when shift_date=cast(getdate() as date) and do_code=1 then '當天入庫'

when shift_date=cast(getdate() as date) and do_code=0 then '當天出庫'

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-2,getdate()) as date) and do_code=1 then '3天入庫'

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-2,getdate()) as date) and do_code=0 then '3天出庫' 

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-7,getdate()) as date) and do_code=1 then '7天入庫'

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-7,getdate()) as date) and do_code=0 then '7天出庫' end as 'shift_date'

,sum(hand_num) 'qty'

from [zws].[dbo].[zws]

where shift_date>='2018-07-10'

group by case when shift_date=cast(getdate() as date) and do_code=1 then '當天入庫'

when shift_date=cast(getdate() as date) and do_code=0 then '當天出庫'

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-2,getdate()) as date) and do_code=1 then '3天入庫'

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-2,getdate()) as date) and do_code=0 then '3天出庫' 

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-7,getdate()) as date) and do_code=1 then '7天入庫'

when shift_date<=cast(getdate() as date) and shift_date>=cast(dateadd(dd,-7,getdate()) as date) and do_code=0 then '7天出庫' end 

) s

查詢最近七天的資料統計

select count 0 count,date format operatetime,m d time,case dayofweek operatetime when 1 then 星期日 when 2 then 星期一 when 3 then 星期二 when 4 then 星期三 when ...

mysql 統計七天資料並分組

統計各機型最近7天bug數量來支撐一張圖表 sql需要查詢最近七天資料並按每天和機型進行分組 select from table where date sub curdate interval 7 day date column time 拓展 查詢最近一天的資料 select from table...

sql語句查詢最近七天 三十天 資料

幾個小時內的資料 date sub now interval 5 hour 今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1 7天 select ...