?
Solved

Oracle SQL to group every 2 hours

Posted on 2007-03-25
5
Medium Priority
?
4,257 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 1000 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

Industry Leaders: 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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

765 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