Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PL/SQL Group by

Posted on 2016-08-11
2
Medium Priority
?
70 Views
Last Modified: 2016-08-11
Hello experts,

So it is needed to know which Owners have the most Protocols
and how many.
A query was crafted

Select owner_ID, SUM(protocol_id) as CNT
from ccm_protocol
group by owner_ID
--having SUM(protocol_id) between 1 and 601000
order by CNT desc

which returned
Owner_ID  CNT
258490      2631271323
306118      2435400026
296588      2261418059
207643      1916887537
212733      1866463289
202729      1729376813
430247      1690954678
213027      1670415116
213978      1601271602
257757      1593747213

Which blew my mind cause I think this says
that Owner_ID 258490 has 261 million protocols.
So the query was input

Select Owner_id, protocol_id
from ccm_protocol
where owner_id = 258490

which returned 1872 rows.

So there is something wrong with the GROUP BY query.

How can I get a list of OWNER_IDs with the number of
Protocols for each OWNER_ID?

Thanks.

Allen in Dallas
0
Comment
Question by:Allen Pitts
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 41752225
you have to count and not to Sum

Select owner_ID, count(protocol_id) as CNT
from ccm_protocol
group by owner_ID
--having SUM(protocol_id) between 1 and 601000
order by CNT desc

Open in new window

0
 

Author Closing Comment

by:Allen Pitts
ID: 41752837
Well done. Thanks.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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…

972 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