有Oracle特色的sql语句整理
有Oracle特色的sql语句整理,通常在做统计分析时我们都想尽可能多滴选择出原始列和统计值列,但是这样group by后面就必须跟随更多
我们知道每个RDBMS在sql方面都会存在自己的特色。那么今天我们来看看Oracle有啥特色值得我们来在意。
特色1 :
Oracle分析函数与开窗函数:
语法:
FUNCTION_NAME (
OVER (
例如:
sum(sal) over (partition by deptno order by ename rows between。。。)
其中,sum是函数名,
Over()是关键字,直接点就是给分析函数加条件,用于识别sum()是聚合函数还是分析函数
说明:
(1)通常在做统计分析时我们都想尽可能多滴选择出原始列和统计值列,但是这样group by后面就必须跟随更多的列,使用分析函数可以避免使用group by时选择出来的列名必须出现在group by列表中的痛苦
(2)聚合函数用group by分组,,每个分组返回一个统计值;分析函数用partition by分组,每组每行都可返回一个统计值。
(3)分析函数带有一个开窗函数over(),含三个分析字句:
分组(partition by)排序(order by)窗口(rows)
(4)两个order by的区别:
分析函数是在整个sql查询后(sql语句的执行比较特殊)再进行的操作,也就是说,sql语句的order by也会影响分析函数的执行结果。
A) 如果sql语句中的order by满足分析函数分析时要求的排序,那么sql语句的排序将先执行,分析函数在分析时就 不必再排序了。
B) 如果sql语句中的order by不满足分析函数分析时要求的排序,那么sql语句中的排序将先执行。
(5)窗口就是分析函数分析时要处理的数据范围:
第一行是:unbounded preceding
当前行是:current row
最后一行是:unbounded following
窗口字句不能单独出现,必须有order by子句时才能出现。而出现order by子句时,不一定要有窗口子句,此时的窗口缺省是第一行到最后一行;
当省略窗口子句时:
A)如果存在order by,则缺省的窗口是unbounded preceding
And current row;
B)如果同时省略order by,则缺省的窗口是unbounded preceding and unbounded
following
例子:
1 统计每个部门工资最高的哪位?
select * from
(
select ename,sal,deptno,rank()over(partition by deptno order by sal desc) mm from emp
)
where mm=1
注意:
1).在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,
row_number()只返回一个结果
2).rank()和dense_rank()的区别是:
--rank()是跳跃排序,有两个第二名时接下来就是第四名
--dense_rank()l是连续排序,有两个第二名时仍然跟着第三名
2 显示各部门员工的工资,并附带该部门的最高工资。
select deptno,empno,ename,sal,last_value(sal)over(partition by deptno order by sal rows
between unbounded preceding and unbounded following)
max_sal from emp;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.
