Solved

Oracle SQL select within select  with calculation

Posted on 2014-04-03
4
439 Views
Last Modified: 2014-04-03
I need help coding some SQL. THERE is one table called B_TABLE.

B_TABLE can have MANY Bills for 1 PROV
 
Each PROV can have many CL,
Each CL FOR A PROVIDER CAN HAVE  1 to many CA
 
Want to show EACH unique PROV where  and their CA / CL ratio
 
PROV       CL           CA      AMT
A                aa           123       10.00
A                aa           234         5.00
A                aa           234       20.00
A               aa            345       15.00
A               dd           456       25.00
A               ee           717       25.00
 
B               bb           877     100.00
B               cc             342    200.00
 
The query results for the example above should be
 
A   1.67    70.00
B   1        300.00
 
I am having trouble coding the SQL.
0
Comment
Question by:ktylerconk
  • 2
4 Comments
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Can you explain how those results were determined and what does the 1.67 represent and if the 300.00 for B represents a sum, why is A 70.00? I think a little more detail of what you want to accomplish would help.
0
 

Author Comment

by:ktylerconk
Comment Utility
Sum of A should be 100.00. Sorry, I added rows.

Ratio of 1.66 is the CA divided by CL.   a flag will be generated if the ratio us greater than 2.
So if A had 6 CA's but only 2 CL's, there would be a flag raised because the ratio would be 3.
0
 
LVL 34

Accepted Solution

by:
johnsone earned 200 total points
Comment Utility
Try this:

select prov, count(distinct ca) / count(distinct cl), sum(amt)
from b_table
group by prov;

Open in new window


It gives the result:

PROV	COUNT(DISTINCTCA)/COUNT(DISTINCTCL)	SUM(AMT)
A       1.666666666667                          100
B       1                                       300

Open in new window

0
 

Author Closing Comment

by:ktylerconk
Comment Utility
Worked like a charm! Thank you!
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.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to take different types of Oracle backups using RMAN.

772 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

12 Experts available now in Live!

Get 1:1 Help Now