每月所有日期的SQL Server动态列和报告输出

时间:2011-01-05 09:19:00

标签: asp.net sql-server-2005 sql-server-2008

我有一个存储过程,我用于asp.net报告,它需要3个参数。

  1. 月份名称,
  2. 分行ID
  3. 这是程序

    ALTER PROCEDURE [dbo].[Report_TabularWholeMonth]   
     @MonthName varchar(20),  
      @Year varchar(20),  
      @Branch_ID integer  
        AS  
      BEGIN  
    
        IF 1=0 BEGIN  
           SET FMTONLY OFF  
      END  
    
      --      Parameters and presets
      --===== This would be the parameter for a stored procedure
     DECLARE @pDesiredMonth DATETIME ;
     SELECT @pDesiredMonth =@MonthName + space(1)+ @Year --'December 2010';
    
     --===== These are some working variables.  Their names tell what they do.
     DECLARE @MonthStart     DATETIME,
          @NextMonthStart DATETIME,
           @Days           INT,
           @SQL            VARCHAR(MAX)
    ;
     SELECT @MonthStart     = DATEADD(mm,DATEDIFF(mm,'1753',@pDesiredMonth),'1753'),
          @NextMonthStart = DATEADD(mm,1,@MonthStart),
           @Days           = DATEDIFF(dd,@MonthStart,@NextMonthStart)
     ;
    
    SELECT DesiredDay  = N,
         DesiredDate = DATEADD(dd,t.N-1,@MonthStart)
        INTO #DesiredDates
     FROM dbo.Tally t
     WHERE N BETWEEN 1 AND @Days
      ;
      -- This pre-aggregates the data and, yeah, it uses an index seek to do so.
      -- It's just easier than joining the dates above with a million row table and the "  reflection" in a CTE
      -- will still cause that join to happen.  Instead, we used "Divide'n'Conquer" on this.
      SELECT VTRCheckList.CLid, CLName,
               TheDay      = DAY(vtrRespDate),
               Total_Count = SUM(Cast(VtrValue as Int))
               INTO #PreAggregation
           FROM VTRCheckListDetails 
            INNER JOIN VTRCheckList on VTRCheckListDetails.CLid=  VTRCheckList.CLid
           WHERE vtrRespDate   >= @MonthStart
          AND vtrRespDate    < @NextMonthStart 
          AND branchid = @Branch_ID
          GROUP BY VTRCheckList.CLid , CLName,vtrRespDate
         ;
        -- This creates the "Task_ID" portion of the report query.
        -- READ about "GROUP BY WITH CUBE" to learn what "GROUPING" does for WITH ROLLUP and WITH CUBE.
                SELECT @SQL = 'SELECT CLName = CASE WHEN GROUPING(CLName) = 1 THEN SPACE(5)+''Total'' ELSE CLName END,'
    
               SELECT @SQL = @SQL + CHAR(10)
                + 'SUM(CASE WHEN date.DesiredDay = ' + CAST(t.DesiredDay AS VARCHAR(2))
    
            -- + ' THEN preagg.Total_Count ELSE 0 END) AS ''' + CASE WHEN   DATEDIFF(dd,0,t.DesiredDate)%7 = 6 THEN ' SomeValue' ELSE    CONVERT(CHAR(6),t.DesiredDate,13) END+''','               
             + ' THEN preagg.Total_Count ELSE 0 END) AS ''' +  CONVERT(CHAR(6),t.DesiredDate,13)+ CASE WHEN DATEDIFF(dd,0,t.DesiredDate)%7 = 6 THEN   '(Sun)' ELSE '' END+''','  
              FROM #DesiredDates t  
    ;
        --===== This creates the total for each Task_ID and finishes up the query with criteria, grouping, etc, etc.
         SELECT @SQL = @SQL + ' Total = SUM(Total_Count)
        FROM #DesiredDates date
      LEFT JOIN #PreAggregation preagg
        ON date.DesiredDay = preagg.TheDay
     WHERE preagg.CLid > 0 
      GROUP BY preagg.CLName --WITH ROLLUP --READ ABOUT GROUP BY, WITH ROLLUP, and WITH    CUBE (for "GROUPING")  
     ORDER BY CLName  
     ;
    '
    ;
    ;
    EXEC(@SQL)
    END
    

    我的问题是它生成了最终的SQL,它根据月份日期和构建列填充数据。在asp.net报告中创建DataSet我需要根据select语句选择列。

    但是在DataSet中它只检索两列CLName和TOTAL。

    如何解决此问题,因为此报告会运行很多用户。

0 个答案:

没有答案