Skip to Main Content

APEX

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sum of timestamp differences using group by function

KthunSep 11 2019 — edited Sep 12 2019

Hi everyone,

from a table recording dates to make reservations for instruments in my lab, I need to generate a simple (dynamic) report with 3 columns in which the total amount of hours reserved for each user for each instrumentation is calculated.

Each reservation record stores a DATE_START and a DATE_END in 'timestamp' format, thus the time for each record is a difference between 2 timestamps.

Here's an example:

IDAPPLICANTPRODUCT_IDDATE_STARTDATE_END
1luca3004-SEP-19 12.00.00.000000 PM04-SEP-19 02.00.00.000000 PM
2mario3104-SEP-19 01.00.00.000000 PM04-SEP-19 02.00.00.000000 PM
3mario3204-SEP-19 02.15.00.000000 PM04-SEP-19 04.00.00.000000 PM
4luca3105-SEP-19 01.00.00.000000 PM05-SEP-19 02.15.00.000000 PM
5luca3105-SEP-19 04.00.00.000000 PM05-SEP-19 04.15.00.000000 PM
6mario3005-SEP-19 01.00.00.000000 PM05-SEP-19 02.15.00.000000 PM
7paolo3106-SEP-19 10.00.00.000000 AM06-SEP-19 02.00.00.000000 PM
8paolo3106-SEP-19 03.00.00.000000 PM06-SEP-19 04.00.00.000000 PM
9paolo3006-SEP-19 05.00.00.000000 AM06-SEP-19 06.10.00.000000 PM
10luca3206-SEP-19 10.00.00.000000 AM06-SEP-19 02.00.00.000000 PM
11luca3206-SEP-19 02.00.00.000000 AM06-SEP-19 04.30.00.000000 PM
12paolo3107-SEP-19 01.00.00.000000 PM07-SEP-19 02.00.00.000000 PM
13mario3207-SEP-19 02.30.00.000000 PM07-SEP-19 04.00.00.000000 PM
14mario3107-SEP-19 05.10.00.000000 PM07-SEP-19 07.00.00.000000 PM
15paolo3007-SEP-19 09.00.00.000000 AM07-SEP-19 02.10.00.000000 PM

The results should be like (I typed random numbers in HOURS column just for example) :

APPLICANTPRODUCT_IDHOURS
Luca306,5
Luca314
Luca323
Mario302,5
Mario315
Mario321,5
Paolo307
Paolo313

I used GROUP BY function to correlate users and products and calculated the difference between timestamps in hours but just for 1 record, e.g.:

select extract( hour from diff) hours

  from (select DATE_END – DATE_START diff FROM RESERVATIONS where ID=411)

Any suggestion please?

Thank you

K

This post has been answered by cormaco on Sep 11 2019
Jump to Answer
Comments
Post Details
Added on Sep 11 2019
6 comments
1,921 views