Solved

Oracle SQL Query

Posted on 2013-10-31
2
333 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PL SQL Search Across Columns 4 35
Character matching different date formats for dates between 6 44
migration MS SQL database to Oracle 30 59
pl/sql - query very slow 26 57
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…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.

815 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

8 Experts available now in Live!

Get 1:1 Help Now