简体   繁体   中英

SQL Oracle Combine records

I have a query which returns the time somebody was at work for. However this gives me multiple records for each person. Is there a way to add together the times so that i have a total time for each employee.

The query is:

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;

The current output is:

在此处输入图片说明

but i would like to combine the Steve Baid values into 1.

Any ideas?

Use || to concat values like:

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

I think you'll need to do this as nested queries:

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: I haven't been able to test or verify this code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM