Skip to Main Content

SQL & PL/SQL

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!

How to sum the values of a column based on minimum of two values in another column ?

msJun 8 2022 — edited Jun 8 2022

Hi Everyone
I am using Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production.
There is a task date, shift and volume column in table.
image.png
I want to sum the volume from the task_date 10 PM to next day 6 AM and display the total for each night shift with the previous day date(i.e the date on which the shift starts at 10 PM )
This needs to be done for a period of date ranges which will be input as part of this SQL query.

for example: Total picked volume for one night shift from 13-07-2022 22:00 to 14-07-2022 02:00 will be 110 for the date 13-jul-2022 and so on.

Please find the create/insert scripts for the sample case. The Shift needs to be created in the query itself.

create table duumy_t
(
task_end_date date,
picked_volume number
);
insert into duumy_t values ('12-jul-2022 01:00:00',20);
insert into duumy_t values ('12-jul-2022 02:00:00',20);
insert into duumy_t values ('12-jul-2022 22:00:00',30);
insert into duumy_t values ('12-jul-2022 23:00:00',40);
insert into duumy_t values ('12-jul-2022 23:00:00',200);
insert into duumy_t values ('13-jul-2022 01:00:00',400);
insert into duumy_t values ('13-jul-2022 01:00:00',20);
insert into duumy_t values ('13-jul-2022 02:00:00',20);
insert into duumy_t values ('13-jul-2022 22:00:00',30);
insert into duumy_t values ('13-jul-2022 23:00:00',40);
insert into duumy_t values ('14-jul-2022 01:00:00',20);
insert into duumy_t values ('14-jul-2022 02:00:00',20);
insert into duumy_t values ('14-jul-2022 22:00:00',20);
insert into duumy_t values ('14-jul-2022 23:00:00',20);
insert into duumy_t values ('14-jul-2022 14:30:00',20);
insert into duumy_t values ('14-jul-2022 10:30:00',20);
commit;

Thanks
Gautam

This post has been answered by Solomon Yakobson on Jun 10 2022
Jump to Answer
Comments
Post Details
Added on Jun 8 2022
7 comments
388 views