search
HomeDatabaseMysql Tutorial用oracle语句计算百分比

用oracle语句计算百分比

Jun 07, 2016 pm 02:50 PM
oraclecompanypercentagefirst daycalculatestatement

来公司第一天,也没什么事干,经理说今天需要把报表做出来,需要写几个oracle语句,用来查询各类别下的人 员 数量和占总人数比例。如下图 统计人数到不是问题,关键是比例不知道该怎么去做,上网查了好几个方式都尝试过了,但还是没能弄出来,一 下 午的时间

 

    来公司第一天,也没什么事干,经理说今天需要把报表做出来,需要写几个oracle语句,用来查询各类别下的人

数量和占总人数比例。如下图

 

       统计人数到不是问题,关键是比例不知道该怎么去做,上网查了好几个方式都尝试过了,但还是没能弄出来,一

午的时间全花在解决这个问题上了,最后在要下班的时候,我突然看到了oracle中聚合函数sum() over的使用,最

终使得问题得到了解决。

 

实现代码:

SELECT(CASE WHEN db_psndoc.age30 THEN '30岁及以下' END)
ranges, COUNT(*) rs ,100*round(COUNT(*)/SUM(COUNT(*)) OVER(),4)||'%' percent FROM bd_psnd
GROUP BY CASE
WHEN bd_psndoc.age
<p><span style="font-size:18px"><strong></strong></span> </p>
<p><span style="font-size:18px"><strong>结果展示:</strong></span></p>
<p><img src="/static/imghwm/default1.png" data-src="http://img.blog.csdn.net/20160508223110629?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" class="lazy" alt=""></p>
<p> </p>
<p> </p>
<p> </p>
<p><span style="font-size:18px">SUM() OVER用法:</span></p>
<p><span style="font-size:18px">参考链接:http://www.2cto.com/database/201209/157605.html</span></p>
<p><span style="font-size:18px"></span> </p>
<p><span style="font-size:18px"><strong>总结:</strong></span></p>
<p><span style="font-size:18px">1、要学习的东西还有很多,知道什么有用很重要。</span></p>
<p><span style="font-size:18px">2、基础知识很重要,能用已知的探索未知的。</span></p>
<p> </p>
<p><span style="font-size:18px"></span> </p>
<p><span style="font-size:18px"></span> </p>
<p><span style="font-size:18px"></span> </p>
<p><span style="font-size:18px"></span> </p>
<p> </p>
   








Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)