Solved

Oracle SQL Query

Posted on 2013-10-31
2
332 Views
Last Modified: 2013-10-31
Hi Experts,

I have the table with below columns
1.      ID
2.      WORK_ID
3.      VOLUME


It has a composite primary key ID and W_ID
Below is the data

ID       W_ID      VOL
1      1      10
2      2      10
3      2      10
3      3      10
1      4      15
3      5      10

Output : I want the ID and volume as out put, but calculating volume has some rules

1.      If Works is shared between ID’s, volume should be equally divided between them.

Sample out put depending on above table is

1 – 10 + 15 = 25
2 – 10/2 = 5 (Work 2 shared by both 2 and 3)
3 – 10/2 + 10 + 10 = 25

I need a query to get the above result.

 
Thanks,
Ravi
0
Comment
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39615995
try this.
select ID,SUM(VOL) VOL
  from (
select t1.ID,t1.W_ID,t1.VOL/t2.cnt VOL
  from test t1 
  join (select W_ID,COUNT(W_ID) cnt from test group by W_ID) t2
    on t1.W_ID = t2.W_ID) t3
 group by ID

Open in new window


http://sqlfiddle.com/#!4/8ceed/12
0
 

Author Closing Comment

by:Ravi Kiran Reddy Katkuri
ID: 39616059
Awsome, thanks a lot for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

861 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