Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

sql - Oracle: how to get percent of total by a query?

I have the following query and its result is very unexpected to me:

select stato, (count(1) *100) / sum(1)
from LOG_BONIFICA
group by stato;

it returns 100 for all rows. Should it be different ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

count(1) is equal to sum(1) in your case.

Try something like this:

18:39:36 SYSTEM@dwal> ed
Wrote file S:\toolsuffer.sql

  1  select owner,
  2         count(*) group_cnt,
  3         sum(count(*)) over() total_cnt,
  4         round(100*(count(*) / sum(count(*)) over ()),2) perc
  5    from dba_objects
  6   group by owner
  7*  order by 4 desc
18:39:57 SYSTEM@dwal> /

OWNER                           GROUP_CNT  TOTAL_CNT       PERC
------------------------------ ---------- ---------- ----------
SYS                                 31609      59064      53.52
PUBLIC                              24144      59064      40.88
XDB                                  1660      59064       2.81
SYSTEM                                597      59064       1.01
WMSYS                                 332      59064        .56
EXFSYS                                312      59064        .53
IRKAZDATA                             158      59064        .27
STRMADMIN                              92      59064        .16
DBSNMP                                 55      59064        .09
RI                                     25      59064        .04
PASS                                   16      59064        .03
POTS                                   19      59064        .03
TI                                     11      59064        .02
STRMODS                                11      59064        .02
OUTLN                                  10      59064        .02
APPQOSSYS                               5      59064        .01
ORACLE_OCM                              8      59064        .01

17 rows selected.

Elapsed: 00:00:00.16

update: or even simplier with ratio_to_report

18:53:36 SYSTEM@dwal> ed
Wrote file S:\toolsuffer.sql

  1  select owner,
  2         round(100*ratio_to_report(count(*)) over (), 2) perc
  3    from dba_objects
  4   group by owner
  5*  order by 2 desc
18:54:03 SYSTEM@dwal> /

OWNER                                PERC
------------------------------ ----------
SYS                                 53.52
PUBLIC                              40.88
XDB                                  2.81
SYSTEM                               1.01
WMSYS                                 .56
EXFSYS                                .53
IRKAZDATA                             .27
STRMADMIN                             .16
DBSNMP                                .09
RI                                    .04
PASS                                  .03
POTS                                  .03
TI                                    .02
STRMODS                               .02
OUTLN                                 .02
APPQOSSYS                             .01
ORACLE_OCM                            .01

17 rows selected.

Elapsed: 00:00:00.20

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...