Solved

Oracle SQL to group every 2 hours

Posted on 2007-03-25
5
4,131 Views
Last Modified: 2013-12-19
I would like to make an SQL script to group shipments every two hours. I want to know how many Tons where shipped every 2 hours.
This script gives the shipped every hour:
  select to_char(weight_out_date,'DD-MM-YYYY HH24') Hours, sum(actual_qty) Shipped from shipments
  where to_char(weight_out_date,'YYMMDDHH24') between '07032408' and '07032507'
 and item_number between 7350 and 7356
  group by to_char(weight_out_date,'DD-MM-YYYY HH24')
0
Comment
Question by:Ehab Salem
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 250 total points
ID: 18789333
group by to_char(weight_out_date,'DD-MM-YYYY HH24')

should become

group by to_char(weight_out_date,'DD-MM-YYYY'), trunc(to_number(to_char(weight_out_date, 'HH24')) / 2)

Apply this for select clause also and voila.
0
 
LVL 15

Expert Comment

by:ishando
ID: 18790847
I don't think a simple divide by 2 will be enough... you also need to drop fractional parts. Try

group by to_char(weight_out_date,'DD-MM-YYYY'), floor(to_char(weight_out_date,'HH24')/2)
0
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 18790945
ishando:

trunc(to_number(to_char(weight_out_date, 'HH24')) / 2) -> trunc gets rid of fractional
0
 
LVL 15

Expert Comment

by:ishando
ID: 18790948
oops, sorry you are right - I should read a bit more carefully!!
0
 
LVL 14

Author Comment

by:Ehab Salem
ID: 18791065
Thanks.

I just added a small modification as I want to display 0-2-4-6... and not 0-1-2-3
 group by trunc(to_number(to_char(weight_out_date, 'HH24')) / 2)*2
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Developer 6 75
oracle RMAN - trying to duplicate a database 5 52
pl/sql - query very slow 26 87
Oracle 12c Default Isolation Level 17 55
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question