SQL Oracle组合记录

时间:2014-11-29 16:02:11

标签: sql oracle

我有一个查询,它返回某人正在工作的时间。然而,这给了我每个人的多个记录。有没有办法将时间加在一起,这样我就有了每个员工的总时间。

查询是:

select 
employee.first_name, 
employee.last_name, 
to_number( to_char(to_date('1','J') +
(time_sheet.finish_date_time - time_sheet.start_date_time), 'J') - 1)  days,
to_char(to_date('00:00:00','HH24:MI:SS') +
(time_sheet.finish_date_time - time_sheet.start_date_time), 'HH24:MI:SS') time
from 
employee 
inner join 
employee_case on employee.employee_id = employee_case.employee 
inner join
time_sheet on time_sheet.employee_case = employee_case.employee_case_id 
where 
employee_case.case = 1;

目前的输出是:

enter image description here

但我想将Steve Baid值合并为1。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

使用||连接值如:

select 
employee.first_name, 
employee.last_name, 
sum (to_number( to_char(to_date('1','J') +
(time_sheet.finish_date_time - time_sheet.start_date_time), 'J') - 1))  days
from 
employee 
inner join 
employee_case on employee.employee_id = employee_case.employee 
inner join
time_sheet on time_sheet.employee_case = employee_case.employee_case_id 
where 
employee_case.case = 1;
GROUP BY employee.first_name,  employee.last_name

答案 1 :(得分:0)

我认为您需要以嵌套查询的方式执行此操作:

select
first_name,
last_name,
to_number( to_char(to_date('1','J') + (duration), 'J') - 1)  days,
to_char(to_date('00:00:00','HH24:MI:SS') + (duration), 'HH24:MI:SS') time
from (
    select
    employee.first_name first_name,
    employee.last_name last_name,
    time_sheet_sum.duration duration
    from
    employee
    inner join
    (
        select
        distinct employee_case.employee_id employee_id,
        sum(time_sheet.finish_date_time - time_sheet.start_date_time) duration
        from
        employee_case
        inner join
        time_sheet on time_sheet.employee_case = employee_case.employee_case_id
        where
        employee_case.case = 1
        group by
        employee_case.employee_id
    ) time_sheet_sum on employee.employee_id = time_sheet_sum.employee
);

NB:我无法测试或验证此代码。