Solved

Oracle SQL to group every 2 hours

Posted on 2007-03-25
5
4,101 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL - Query help 7 66
Import and exporting Oracle Data with encrypted columns 4 48
ORA-02288: invalid OPEN mode 2 56
sum of columns in a row in oracle 3 32
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

792 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