Solved

Oracle SQL to group every 2 hours

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

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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 explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now