sql server 查詢本週、本月全部天數的數據

查詢本月全部的天數:get

--本月全部的天數
select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) day from 
(select substring(convert(varchar,GETDATE(),120),1,7)+'-01' day) t1, 
(select number from MASTER..spt_values WHERE TYPE='P' AND number>=0 and number<=31) t2 
where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like substring(convert(varchar,GETDATE(),120),1,7)+'%'

 

查詢本週全部的天數:string

select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 0),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 1),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 2),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 3),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 4),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 5),120)
union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 6),120)


示例:it

以下圖所示,有表MO_Orders,字段:BookTime,Number,Count,有以下數據。io

 

查詢出本週的天天的數據總和,語句以下:date

with t as 
( 
    select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 0),120)
    union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 1),120)
    union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 2),120)
    union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 3),120)
    union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 4),120)
    union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 5),120)
    union all select date=convert(varchar(10),dateadd(wk, datediff(wk,0,getdate()), 6),120)
)
select id=ROW_NUMBER()OVER(ORDER BY t1.date),
        DATENAME(weekday,t1.date) as weekday,
        t1.date,
        Numbers=sum(isnull(t2.Number,0)),  
        Count=sum(isnull(t2.Count,0)) 
from t t1 
left join
(  
    select substring(convert(varchar,BookTime,120),1,11) as BookTime,
            Number,Count 
    from  MO_Orders 
    where datediff(wk,BookTime-1,getdate()) = 0  
    )
    t2 
on t1.date= substring(convert(varchar,t2.BookTime,120),1,11)
group by t1.date

 

查詢效果以下圖,其中 weekday爲星期,此圖還需替換,稍後補上:
select

 

示例:以下圖所示,有表: MO_Orders,字段:BookTime,Cost,Count 。nio

查詢本月的全部數據總和(其中:total=Cost*Count)。im

查詢出本月的天天的數據總和,顯示天天的,查詢語句以下:數據

with t as 
( 
  select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) date from 
    (select substring(convert(varchar,GETDATE(),120),1,7)+'-01' day) t1, 
    (select number from MASTER..spt_values WHERE TYPE='P' AND number>=0 and number<=31) t2 
    where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like substring(convert(varchar,GETDATE(),120),1,7)+'%'
)
select id=ROW_NUMBER()OVER(ORDER BY t1.date),
        t1.date,
        CostTotal=sum(isnull(t2.CostTotal,0))
from t t1 
left join 
(    
    select BookTime,sum(CostTotal) as CostTotal from 
    (
        select substring(convert(varchar,BookTime,120),1,11) as BookTime,
                Cost*Count as CostTotal 
        from  MO_Orders 
        where datediff(month,BookTime,getdate()) = 0 
    ) o group by BookTime
) t2
on t1.date= t2.BookTime 
group by t1.date

 

查詢結果以下圖:
查詢

相關文章
相關標籤/搜索