Sql时钟报告 - 显示星期几的数据

时间:2013-12-04 13:17:07

标签: sql tsql

新手问题....我运行以下查询:

SELECT EMPLOYEE.EMPLOYEEID, EMPLOYEE.FORENAMES,
       EMPLOYEE.SURNAME, 
     -- CLOCKING.CLOCKINGDATETIMEUNROUNDED,
     CONVERT(VARCHAR(5),CLOCKING.CLOCKINGDATETIMEUNROUNDED,108) AS CLOCKTIME,
     CONVERT(VARCHAR(10),CLOCKING.CLOCKINGDATETIMEUNROUNDED,103) AS CLOCKDATE,
     DATENAME(WEEKDAY,CLOCKING.CLOCKINGDATETIMEUNROUNDED) AS WHICHDAY,
       CASE 
        WHEN CLOCKING.DAYREVIEWINOUTFLAG = '1' THEN 'OUT'
        WHEN CLOCKING.DAYREVIEWINOUTFLAG = '0' THEN 'IN'
        ELSE '???'
     END AS CLOCKTYPE
     --, TIMECLOCK.TIMECLOCKDESCRIPTION
FROM   CLOCKING 
       INNER JOIN
       EMPLOYEE ON CLOCKING.EMPLOYEECODE = EMPLOYEE.EMPLOYEECODE
     INNER JOIN
     TIMECLOCK ON CLOCKING.TIMECLOCKCODE = TIMECLOCK.TIMECLOCKCODE
WHERE  CLOCKING.CLOCKINGDATETIMEUNROUNDED >= '2013-11-11'
   AND CLOCKING.CLOCKINGDATETIMEUNROUNDED < '2013-11-18'
   AND CLOCKING.DUPLICATECLOCKINGFLAG  ='0'

然后返回

EMPLOYEEID  FORENAMES   SURNAME CLOCKTIME   CLOCKDATE   WHICHDAY    CLOCKTYPE
1001        JOE         BLOGGS  06:42       11/11/13    Monday      IN
1001        JOE         BLOGGS  17:32       11/11/13    Monday      OUT
1001        JOE         BLOGGS  06:41       12/11/13    Tuesday     IN
1001        JOE         BLOGGS  17:31       12/11/13    Tuesday     OUT
1001        JOE         BLOGGS  06:42       13/11/13    Wednesday   IN
1001        JOE         BLOGGS  17:31       13/11/13    Wednesday   OUT

然而,我真正想要输出的是......

EMPLOYEEID  FORENAMES   SURNAME CLOCKTYPE   MONDAY  TUESDAY WEDNESDAY
1001        JOE         BLOGGS  IN          06:42   06:41   06:42
1001        JOE         BLOGGS  OUT         17:32   17:31   17:31

这可能吗?

1 个答案:

答案 0 :(得分:1)

你需要学习和理解PIVOT关系运算符(参见上面的@Yuck的评论)。

然而,这应该让你接近你想要的地方:

我不应该如何汇总进出的个人日期,所以我违约了MIN。

DECLARE @CLOCKING AS TABLE ( TIMECLOCKCODE INT
                            ,EMPLOYEECODE INT
                            ,CLOCKINGDATETIMEUNROUNDED DATETIME
                            ,DAYREVIEWINOUTFLAG BIT
                            ,DUPLICATECLOCKINGFLAG BIT)
DECLARE @EMPLOYEE AS TABLE (EMPLOYEEID INT
                            , EMPLOYEECODE INT
                            , FORENAMES VARCHAR(50)
                            , SURNAME VARCHAR(50))
DECLARE @TIMECLOCK AS TABLE (TIMECLOCKCODE INT)

INSERT INTO @CLOCKING VALUES (1,1001,'11 NOV 2013 06:42',0,0)
INSERT INTO @CLOCKING VALUES (1,1001,'11 NOV 2013 17:32',1,0)
INSERT INTO @CLOCKING VALUES (1,1001,'12 NOV 2013 06:41',0,0)
INSERT INTO @CLOCKING VALUES (1,1001,'12 NOV 2013 17:31',1,0)
INSERT INTO @CLOCKING VALUES (1,1001,'13 NOV 2013 06:42',0,0)
INSERT INTO @CLOCKING VALUES (1,1001,'13 NOV 2013 17:31',1,0)

INSERT INTO @EMPLOYEE VALUES (1,1001,'JOE','BLOGGS')

INSERT INTO @TIMECLOCK VALUES (1)

SELECT EMPLOYEEID
    ,   FORENAMES
    ,   SURNAME
    ,   CLOCKTYPE
    ,   MIN(Monday) AS Monday
    ,   MIN(Tuesday) AS Tuesday
    ,   MIN(Wednesday) AS Wednesday
    ,   MIN(Thursday) AS Thursday
    ,   MIN(Friday) AS Friday
    ,   MIN(Saturday) AS Saturday
    ,   MIN(Sunday) AS Sunday
FROM
(
SELECT E.EMPLOYEEID, 
       E.FORENAMES,
       E.SURNAME, 
     CONVERT(VARCHAR(5),C.CLOCKINGDATETIMEUNROUNDED,108) AS CLOCKTIME,
     CONVERT(VARCHAR(10),C.CLOCKINGDATETIMEUNROUNDED,103) AS CLOCKDATE,
     DATENAME(WEEKDAY,C.CLOCKINGDATETIMEUNROUNDED) AS WHICHDAY,
       CASE 
        WHEN C.DAYREVIEWINOUTFLAG = '1' THEN 'OUT'
        WHEN C.DAYREVIEWINOUTFLAG = '0' THEN 'IN'
        ELSE '???'
     END AS CLOCKTYPE
FROM   @CLOCKING C 
INNER JOIN @EMPLOYEE E
    ON C.EMPLOYEECODE = E.EMPLOYEECODE
INNER JOIN @TIMECLOCK TC
    ON C.TIMECLOCKCODE = TC.TIMECLOCKCODE
WHERE  C.CLOCKINGDATETIMEUNROUNDED >= '2013-11-11'
   AND C.CLOCKINGDATETIMEUNROUNDED < '2013-11-18'
   AND C.DUPLICATECLOCKINGFLAG  ='0'
) src
PIVOT
(MIN(CLOCKTIME)
FOR WHICHDAY IN ([Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [Sunday]))pvt
GROUP BY  EMPLOYEEID,   FORENAMES,  SURNAME,    CLOCKTYPE