使用GridView和Group By管理数据

时间:2011-05-20 20:28:57

标签: c# asp.net sql sql-server gridview

我正在使用ASP.NET(C#)GridView和带有Sql Server后端的模板字段来处理时间表报告应用程序。我基本上抓住员工一周的任务列表,使用RowDataBound循环访问GridView并检查EndTime是否落在Sun - Sat上,然后将那些小时/分钟放在该列标签中。您可以在下面看到我的GridView标题的示例(抱歉格式化)。

类型|小时|分钟|开始时间|结束时间|太阳|周一|周二|周三|周四|周五|周六|总计

问题是我的groupby没有将我的任务分组在一起,因为我的StartTime / EndTimes将是唯一的,但我需要将它们拉回来,以便我可以检查它属于哪一天,然后填充该列(如您所见)它返回的两行)。我想可能有一种方法可以创建第二个GridView并根据第一个Gridview中的某些值填充它。有没有其他人偶然发现这样的问题?

SELECT 
C.Task,
CONVERT(VARCHAR(10), A.StartTime, 101) As StartTime,
CONVERT(VARCHAR(10), A.EndTime, 101) As EndTime,
SUM(DATEDIFF(n, A.StartTime, A.EndTime)) / 60 AS Hours,
SUM(DATEDIFF(n, A.StartTime, A.EndTime)) % 60 AS Minutes
FROM dbo.Timesheet A, dbo.Employees B,
dbo.TimeSheet_Master C
WHERE A.CreatedBy = B.ContactId
AND A.Task = C.Task
AND (B.ContactId =@ContactId) 
AND (A.StartTime >= @StartTime) 
AND (A.EndTime <= @EndTime) 
GROUP BY CONVERT(VARCHAR(10), A.StartTime, 101), CONVERT(VARCHAR(10), A.EndTime, 101), C.Task;

Task    StartTime   EndTime         Hours   Minutes
-------------------------------------------------------
Install 05/17/2011  05/17/2011      1        0
Install 05/18/2011  05/18/2011      1        30

2 个答案:

答案 0 :(得分:0)

您需要的是按周分组。查看here以获取完整解决方案,如果您需要有关完整解决方案的帮助,请告诉我们。

答案 1 :(得分:0)

我不是100%确定您的要求是什么,但这几乎就像您想要组合一个数据透视表。

我很快就这样做了,所以它可能不太适合你的需要,但无论如何......

我创建了一个名为#timesheet的临时表,并添加了一些值:

CREATE TABLE #timesheet
(
  Task VARCHAR(10),
  employee INT,
  StartDate DATETIME,
  endDate DATETIME
)

 INSERT  INTO #timesheet (Task, employee, StartDate, endDate) VALUES ('Task1',10,'2011-5-10 17:00:00','2011-5-10 20:00:00' ) 
 INSERT  INTO #timesheet (Task, employee, StartDate, endDate) VALUES ('Task1', 10, '2011-5-12 17:00:00', '2011-5-12 20:00:00' ) 
 INSERT  INTO #timesheet (Task, employee, StartDate, endDate) VALUES  ('Task2',10,'2011-5-12 17:00:00','2011-5-12 20:00:00' )         
 INSERT  INTO #timesheet (Task, employee, StartDate, endDate) VALUES  ('Task1', 12, '2011-5-08 17:00:00', '2011-5-08 20:00:00'  ) 

我创建了一个取自Kodyaz Development Services的GetWeekDayNameOfDate函数来获取星期几。

我首先创建了一个摘要查询,如下所示:

SELECT  #timesheet.Task,
    #timesheet.employee,
    MIN(#timesheet.StartDate) AS [Start Date],
    MAX(#timesheet.endDate) AS [End Date],
    SUM(DATEDIFF(n, #timesheet.StartDate, #timesheet.endDate)) / 60 AS [Hours],
    SUM(DATEDIFF(n, #timesheet.StartDate, #timesheet.endDate)) % 60 AS [Minutes]
FROM    #timesheet
GROUP BY #timesheet.Task,
        #timesheet.employee

这会给我我对任务和员工的总结。它将显示整个持续时间的开始日期和结束日期以及小时数和分钟数。

我的结果将是:

Task       employee    Start Date              End Date                Hours       Minutes
---------- ----------- ----------------------- ----------------------- ----------- -----------
Task1      10          2011-05-10 17:00:00.000 2011-05-12 20:00:00.000 6           0
Task2      10          2011-05-12 17:00:00.000 2011-05-12 20:00:00.000 3           0
Task1      12          2011-05-08 17:00:00.000 2011-05-08 20:00:00.000 3           0

我接下来创建了一个详细信息数据透视查询,以总结一周中每一天所花费的小时数。

 SELECT Task,
    Employee,
    ISNULL([Sunday], 0) AS Sunday,
    ISNULL([Monday], 0) AS Monday,
    ISNULL([Tuesday], 0) AS Tuesday,
    ISNULL([Wednesday], 0) AS Wendesday,
    ISNULL([Thursday], 0) AS Thursday,
    ISNULL([Friday], 0) AS Friday,
    ISNULL([Saturday], 0) AS Saturday
 FROM   ( SELECT    #timesheet.Task,
                #timesheet.employee,
                dbo.GetWeekDayNameOfDate(#timesheet.StartDate) AS [Day],
                SUM(DATEDIFF(n, #timesheet.StartDate, #timesheet.endDate))
                / 60 AS [Hours],
                SUM(DATEDIFF(n, #timesheet.StartDate, #timesheet.endDate))
                % 60 AS [Minutes]
      FROM      #timesheet
      GROUP BY  #timesheet.Task,
                #timesheet.employee,
                dbo.GetWeekDayNameOfDate(#timesheet.StartDate)
    ) p PIVOT ( SUM(Hours) FOR [DAY] IN ( [Sunday], [Monday], [Tuesday],
                                          [Wednesday], [Thursday],
                                          [Friday], [Saturday] ) ) AS PivotTable

我的结果将是:

Task       Employee    Sunday      Monday      Tuesday     Wendesday   Thursday    Friday      Saturday
---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
Task1      10          0           0           3           0           3           0           0
Task1      12          3           0           0           0           0           0           0
Task2      10          0           0           0           0           3           0           0

Aftwards我一起加入了我的两个问题以获得整个图片

SELECT  summary.Task,
    summary.employee,
    summary.[Start Date],
    summary.[End Date],
    details.Sunday,
    details.Monday,
    details.Tuesday,
    details.Wendesday,
    details.Thursday,
    details.Friday,
    details.Saturday,
    summary.Hours,
    summary.Minutes
 FROM    ( SELECT    #timesheet.Task,
                #timesheet.employee,
                MIN(#timesheet.StartDate) AS [Start Date],
                MAX(#timesheet.endDate) AS [End Date],
                SUM(DATEDIFF(n, #timesheet.StartDate, #timesheet.endDate))
                / 60 AS [Hours],
                SUM(DATEDIFF(n, #timesheet.StartDate, #timesheet.endDate))
                % 60 AS [Minutes]
      FROM      #timesheet
      GROUP BY  #timesheet.Task,
                #timesheet.employee
    ) summary
    INNER JOIN ( SELECT Task,
                        Employee,
                        ISNULL([Sunday], 0) AS Sunday,
                        ISNULL([Monday], 0) AS Monday,
                        ISNULL([Tuesday], 0) AS Tuesday,
                        ISNULL([Wednesday], 0) AS Wendesday,
                        ISNULL([Thursday], 0) AS Thursday,
                        ISNULL([Friday], 0) AS Friday,
                        ISNULL([Saturday], 0) AS Saturday
                 FROM   ( SELECT    #timesheet.Task,
                                    #timesheet.employee,
                                    dbo.GetWeekDayNameOfDate(#timesheet.StartDate) AS [Day],
                                    SUM(DATEDIFF(n, #timesheet.StartDate,
                                                 #timesheet.endDate)) / 60 AS [Hours],
                                    SUM(DATEDIFF(n, #timesheet.StartDate,
                                                 #timesheet.endDate)) % 60 AS [Minutes]
                          FROM      #timesheet
                          GROUP BY  #timesheet.Task,
                                    #timesheet.employee,
                                    dbo.GetWeekDayNameOfDate(#timesheet.StartDate)
                        ) p PIVOT ( SUM(Hours) FOR [DAY] IN ( [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday] ) ) AS PivotTable
               ) details ON summary.Task = details.Task
                            AND summary.Employee = details.Employee

通过这样做我会得到以下结果:

Task       employee    Start Date              End Date                Sunday      Monday      Tuesday     Wendesday   Thursday    Friday      Saturday    Hours       Minutes
---------- ----------- ----------------------- ----------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
Task1      10          2011-05-10 17:00:00.000 2011-05-12 20:00:00.000 0           0           3           0           3           0           0           6           0
Task1      12          2011-05-08 17:00:00.000 2011-05-08 20:00:00.000 3           0           0           0           0           0           0           3           0
Task2      10          2011-05-12 17:00:00.000 2011-05-12 20:00:00.000 0           0           0           0           3           0           0           3           0

请注意,我几乎忽略了会议记录部分,但我希望至少这对您有所帮助。