Datediff可以导致我无法在SQL中执行我的语句?

时间:2016-06-27 01:45:23

标签: sql firebird

示例数据:

员工

EMPL_KODE | EMPL_NAME |SEX    |SECTION
001       | Michel    |Male   |HR
002       | Clara     |Female |GA
003       | Rafael    |Male   |HR

出勤

EMPL_KODE | EMPL_NAME |DATE_IN    |TIME_IN |TIME_OUT
001       | Michel    |25.04.2016 |06:50   |15:40
002       | Clara     |25.04.2016 |06:15   |15:43
003       | Rafael    |25.04.2016 |06:25   |15:45
001       | Michel    |26.04.2016 |06:23   |15:42
002       | Clara     |26.04.2016 |06:10   |15:41
003       | Rafael    |26.04.2016 |06:30   |15:42
001       | Michel    |27.04.2016 |06:33   |15:42
002       | Clara     |27.04.2016 |06:54   |15:44
003       | Rafael    |27.04.2016 |07:00   |15:45

从这两个表中,我想:

  1. 加入employeeattendance表格

  2. 显示每个Empl_Kode

  3. 的总分钟数或小时数

    我希望输出如下:

    EMPL_KODE | EMPL_NAME | TIME_IN | TIME_OUT | TOTAL_MINUTES | TOTAL_HOURS
    

    SQL代码:

    SELECT 
        EMPLOYEE.EMPL_KODE, EMPLOYEE.EMPL_NAME,
        CAST(ATTANDANCE.DATE_IN + ATTANDANCE.TIME_IN AS TIMESTAMP) AS   "TIME_IN", 
        CAST(ATTENDANCE.DATE_IN + ATTENDANCE.TIME_OUT AS TIMESTAMP) AS "TIME_OUT", 
        SUM(DATEDIFF(MINUTE, ATTENDANCE.TIME_IN, ATTENDANCE.TIME_OUT)) AS "TOTAL_MINUTES",
        SUM(DATEDIFF(HOUR, ATTENDANCE.TIME_IN, ATTENDANCE.TIME_OUT)) AS "TOTAL_HOURS"
    FROM 
        EMPLOYEE
    JOIN 
        ATTENDANCE ON EMPLOYEE.EMPL_KODE = ATTENDANCE.EMPL_KODE
    GROUP BY 
        EMPLOYEE.EMPL_KODE, EMPLOYEE.EMPL_NAME;
    

    我收到错误消息:

      

    选择列表中的表达式无效(不包含在   聚合函数或GROUP BY子句)

    我的代码出了什么问题?你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT 
   EMPLOYEE.EMPL_KODE, 
   EMPLOYEE.EMPL_NAME,
   MIN(CAST(ATTANDANCE.DATE_IN + ATTANDANCE.TIME_IN AS TIMESTAMP)) AS "TIME_IN", 
   MAX(CAST(ATTANDANCE.DATE_IN + ATTANDANCE.TIME_OUT AS TIMESTAMP)) AS "TIME_OUT",
   SUM(DATEDIFF(MINUTE,ATTANDANCE.TIME_IN,ATTANDANCE.TIME_OUT)) AS "TOTAL_MINUTES",
   SUM(DATEDIFF(HOUR,ATTANDANCE.TIME_IN,ATTANDANCE.TIME_OUT)) AS "TOTAL_HOURS"
FROM EMPLOYEE
JOIN ATTANDANCE
     ON EMPLOYEE.EMPL_KODE=ATTANDANCE.EMPL_KODE
GROUP BY EMPLOYEE.EMPL_KODE, EMPLOYEE.EMPL_KODE;