Solved

Oracle SQL to group every 2 hours

Posted on 2007-03-25
5
4,186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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 about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

729 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