选择生成日报表,月报表,年报表

传入参数:

@Type:类型,是哪一种报表,有year,month,day三种

@Time:时间 

根据Type参数分别获取Time的月份数据,日期数据

      declare @Type nvarchar(20) ='year';
    declare @Time DateTime =getdate();
 SELECT  distinct 
   case @Type when 'year' then format(dateadd(mm,number,@Time),'MM月')
        when 'month' then format(dateadd(dd,number,@Time),'dd日')
        else format(dateadd(dd,number,@Time),'dd日') 
        end DT
    FROM master..spt_values
        WHERE type='P' and  ((@Type='day' and number<1) or (@Type!='day' and number<50))

年的时候显示12个月

月的时候显示该月的天数

日的时候就显示该天

注意上面的日的时候,进行判断,number<1

选择生成日报表,月报表,年报表_第1张图片

先查出这个,然后与数据库中表left join

  declare @Type nvarchar(20) ='month';
    declare @Time DateTime =getdate();
  select 
       case @Type when 'year' then format(CreateTime,'MM月')
        when 'month' then format(CreateTime,'dd日')
        else format(CreateTime,'dd日') 
        end DT2,Sum
   (
      isnull(ElectricalLaborHour,0)+
      isnull(ElectricalParts,0)+
      isnull(SheetSprayLaborHour,0)+
      isnull(SheetSprayParts,0)+
      isnull(SheetSprayTransLaborHour,0)+
      isnull(OilChangeLaborHour,0)+
      isnull(OilChangeParts,0)+
      isnull(WarrantyLaborHour,0)+
      isnull(WarrantyParts,0)+
      isnull(WarrantyTransLaborHour,0)+
      isnull(InternalElectricalLaborHour,0)+
      isnull(InternalParts,0)+
      isnull(InternalSheetSprayLaborHour,0)) as Total from T_DMSMaintenance where IsDelete=0 and 
       ((@Type='year' and datepart(yyyy,CreateTime)=datepart(yyyy,@Time)) or
   (@Type='month' and format(CreateTime,'yyyy年MM月')=format(@Time,'yyyy年MM月')) or
  (@Type='day' and format(CreateTime,'yyyy-MM-dd')=format(@Time,'yyyy-MM-dd')))
   group by  case @Type when 'year' then format(CreateTime,'MM月')
        when 'month' then format(CreateTime,'dd日')
        else format(CreateTime,'dd日') end

选择生成日报表,月报表,年报表_第2张图片

看上面的查询条件

or,and联合使用,并且根据具体的Type参数进行分组

然后再将两个表进行连接

declare @Type nvarchar(20) ='month';
  declare @Time DateTime =getdate();
     select DT,Total from (SELECT  distinct 
   case @Type when 'year' then format(dateadd(mm,number,@Time),'MM月')
        when 'month' then format(dateadd(dd,number,@Time),'dd日')
        else format(dateadd(dd,number,@Time),'dd日') 
        end DT
    FROM master..spt_values
        WHERE type='P' and  ((@Type='day' and number<1) or (@Type!='day' and number<50))) as T1 left join
   (select 
       case @Type when 'year' then format(CreateTime,'MM月')
        when 'month' then format(CreateTime,'dd日')
        else format(CreateTime,'dd日') 
        end DT2,Sum
   (
      isnull(ElectricalLaborHour,0)+
      isnull(ElectricalParts,0)+
      isnull(SheetSprayLaborHour,0)+
      isnull(SheetSprayParts,0)+
      isnull(SheetSprayTransLaborHour,0)+
      isnull(OilChangeLaborHour,0)+
      isnull(OilChangeParts,0)+
      isnull(WarrantyLaborHour,0)+
      isnull(WarrantyParts,0)+
      isnull(WarrantyTransLaborHour,0)+
      isnull(InternalElectricalLaborHour,0)+
      isnull(InternalParts,0)+
      isnull(InternalSheetSprayLaborHour,0)) as Total from T_DMSMaintenance where IsDelete=0 and 
       ((@Type='year' and datepart(yyyy,CreateTime)=datepart(yyyy,@Time)) or
   (@Type='month' and format(CreateTime,'yyyy年MM月')=format(@Time,'yyyy年MM月')) or
  (@Type='day' and format(CreateTime,'yyyy-MM-dd')=format(@Time,'yyyy-MM-dd')))
   group by  case @Type when 'year' then format(CreateTime,'MM月')
        when 'month' then format(CreateTime,'dd日')
        else format(CreateTime,'dd日') end) as T2 on T1.DT=T2.DT2

 

选择生成日报表,月报表,年报表_第3张图片

 

你可能感兴趣的:(选择生成日报表,月报表,年报表)