有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



Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.
