如何在同一个GridView中按天展示两周?

时间:2014-05-26 16:02:08

标签: sql sql-server-2008 pivot-table common-table-expression

我正在使用此查询来获取每个员工的两周工资总额。

select FullName
,[Sunday] = SUM([Sunday])
,[Monday] = SUM([Monday])
,[Tuesday] = SUM([Tuesday])
,[Wednesday] = SUM([Wednesday])
,[Thursday] = SUM([Thursday])
,[Friday] = SUM([Friday])
,[Saturday] = SUM([Saturday])
, Total=SUM(ISNULL([Sunday],0)+ISNULL([Monday],0)+ISNULL([Tuesday],0)+ISNULL([Wednesday],0)+ISNULL([Thursday],0)+ISNULL([Friday],0)+ISNULL([Saturday],0))
   from  
    (Select UserId_Fk,ISNULL(CAST(CountHours as decimal(18,2)),0)as CountHours,[Day],CheckIn 
    from CheckInCheckOut)
    as convertedtable
    inner join Users
    on convertedtable.UserId_Fk=Users.UserId
 PIVOT
(
 SUM(CountHours) 
 FOR Day
 IN([Sunday],[Monday],[Tuesday],[Wednesday],[Thursday],[Friday],[Saturday])
)
as PivotTable where CheckIn between @startdate and @enddate 
GROUP BY FullName
end

输出如下:

enter image description here

如何更新我的查询以将其显示为:

 Monday Tuesday Wednesday Thursday Friday Saturday Sunday Monday Tuesday Wednesday Tursday Friday Saturday **Total**

数据库中的数据是:

enter image description here

修改 我有一个名为Paperiod的表,其中包含当年所有付费期的列表:

表的第一行是:

    Monday  21 Apr 2014 - 4 May 2014    

 ![enter image description here][3]

示例将不胜感激。

2 个答案:

答案 0 :(得分:0)

从查询开始,该查询计算从支付期开始到员工工作日的天数。

select *, 
  DateDiff(day, @start_date, Day_Worked) as Day_Of_Period 
  from WorkHours 

在此示例中,新字段Day_Of_Period将是从0到13的数字。

然后根据该新字段生成枢轴。

答案 1 :(得分:0)

愿这个帮助

SELECT 
    t1.FullName
    ,t1.[Monday]
    ,t1.[Tuesday]
    ,t1.[Wednesday]
    ,t1.[Thursday]
    ,t1.[Friday]
    ,t1.[Saturday]
    ,t1.[Sunday]
    ,t2.[Monday]
    ,t2.[Tuesday]
    ,t2.[Wednesday]
    ,t2.[Thursday]
    ,t2.[Friday]
    ,t2.[Saturday]
    ,t2.[Sunday]
    ,(t1.Total + t2.Total) AS Total
FROM 
    (
        SELECT FullName
            ,[Sunday] = SUM([Sunday])
            ,[Monday] = SUM([Monday])
            ,[Tuesday] = SUM([Tuesday])
            ,[Wednesday] = SUM([Wednesday])
            ,[Thursday] = SUM([Thursday])
            ,[Friday] = SUM([Friday])
            ,[Saturday] = SUM([Saturday])
            ,Total = SUM(ISNULL([Sunday], 0) + ISNULL([Monday], 0) + ISNULL([Tuesday], 0) + ISNULL([Wednesday], 0) + ISNULL([Thursday], 0) + ISNULL([Friday], 0) + ISNULL([Saturday], 0))
        FROM (
            SELECT UserId_Fk
                ,ISNULL(CAST(CountHours AS DECIMAL(18, 2)), 0) AS CountHours
                ,[Day]
                ,CheckIn
            FROM CheckInCheckOut
            ) AS convertedtable
        INNER JOIN Users ON convertedtable.UserId_Fk = Users.UserId
        PIVOT(SUM(CountHours) FOR Day IN (
                    [Sunday]
                    ,[Monday]
                    ,[Tuesday]
                    ,[Wednesday]
                    ,[Thursday]
                    ,[Friday]
                    ,[Saturday]
                    )) AS PivotTable
        WHERE CheckIn BETWEEN @startdate
                AND dateadd(day, 7, @startdate)
        GROUP BY FullName
    ) AS t1
INNER JOIN 
    (
        SELECT FullName
            ,[Sunday] = SUM([Sunday])
            ,[Monday] = SUM([Monday])
            ,[Tuesday] = SUM([Tuesday])
            ,[Wednesday] = SUM([Wednesday])
            ,[Thursday] = SUM([Thursday])
            ,[Friday] = SUM([Friday])
            ,[Saturday] = SUM([Saturday])
            ,Total = SUM(ISNULL([Sunday], 0) + ISNULL([Monday], 0) + ISNULL([Tuesday], 0) + ISNULL([Wednesday], 0) + ISNULL([Thursday], 0) + ISNULL([Friday], 0) + ISNULL([Saturday], 0))
        FROM (
            SELECT UserId_Fk
                ,ISNULL(CAST(CountHours AS DECIMAL(18, 2)), 0) AS CountHours
                ,[Day]
                ,CheckIn
            FROM CheckInCheckOut
            ) AS convertedtable
        INNER JOIN Users ON convertedtable.UserId_Fk = Users.UserId
        PIVOT(SUM(CountHours) FOR Day IN (
                    [Sunday]
                    ,[Monday]
                    ,[Tuesday]
                    ,[Wednesday]
                    ,[Thursday]
                    ,[Friday]
                    ,[Saturday]
                    )) AS PivotTable
        WHERE CheckIn BETWEEN dateadd(day, 7, @startdate)
                AND @enddate
        GROUP BY FullName
    ) AS t2 ON t1.FullName = t2.FullName