Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle SQL Query

Posted on 2013-10-31
2
Medium Priority
?
340 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
[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 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

715 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