SQL语法中的SQL语法错误

时间:2018-05-27 02:33:02

标签: mysql sql sql-server

我有一张格式如下的表格。

EMP NUMBER, ATT DATE,   ATT TIME
00001   01-03-2018  08:45
00001   01-03-2018  20:50
00001   02-03-2018  08:00
00001   02-03-2018  18:50

我被要求得到以下结果:

EMP NUMBER,  ATT DATE,    IN TIME,   OUT TIME
00001      01-03-2018   08:45     20:50
00001      02-03-2018   08:00     18:50

我设法使用以下查询:

SELECT EMP_NUMBER, ATT_DATE,MIN(ATT_TIME) AS IN_TIME,MAX(ATT_TIME) AS OUT_TIME 
        INTO TABLE1
FROM FJ 
GROUP BY EMP_NUMBER,ATT_DATE

即使我得到了我想要的答案,但我很担心员工在晚上10点之前登录并在第二天早上5点离开,因此我尝试了以下内容:

SELECT EMP_NUMBER, ATT_DATE,
        CASE
            WHEN ATT_TIME >= 12
            THEN MIN(ATT_TIME)
            ELSE MAX(ATT_TIME)
            AS IN_TIME,
        CASE
            WHEN ATT_TIME < 12
                THEN MIN(ATT_TIME)
                ELSE MAX(ATT_TIME)
         AS OUT_TIME,
         INTO TABLE1
    FROM FJ 
    GROUP BY EMP_NUMBER,ATT_DATE

在&#34; AS&#34;附近引发语法错误,你能帮我弄清楚我哪里出错吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

不确定查询逻辑但是你缺少&#34; END&#34;两个CASE语句的子句。

SELECT EMP_NUMBER, ATT_DATE,
        CASE
            WHEN ATT_TIME >= 12
            THEN MIN(ATT_TIME)
            ELSE MAX(ATT_TIME)
         END               --<-- You are missing "END" here 
            AS IN_TIME,
        CASE
            WHEN ATT_TIME < 12
                THEN MIN(ATT_TIME)
                ELSE MAX(ATT_TIME)
         END                    --<-- and here 
         AS OUT_TIME  --<-- also you had an extra comma here
         INTO TABLE1
    FROM FJ 
    GROUP BY EMP_NUMBER,ATT_DATE