Oracle Advanced Query

Source: Internet
Author: User

1. Random return records

 

Select * from (select ename, job from EMP order by dbms_random.value () where rownum <= 5 returns five records at random. The system function dbms_random.value () is used as a random number.

 

2. Sort null values

Select * from EMP order by comm DESC nulls last; null values rank first

Select * from EMP order by comm DESC nulls first );

3. Continuous summation

Select ename, Sal, sum (SAL) over (),Sum (SAL) over (order by ename)From EMP;

Add over () as a continuous sum. Over ([partition clause] [sorting clause [Sliding Window clause]). If no parameter is added to over, the effect is the same as that of the previous aggregate function. If there are multiple over entries, the sorting order is based on the last one.

Select deptno, Sal,Sum (SAL) over (partition by deptno order by ename)As S from EMP; Continuous Division sum

Follow a field to perform continuous summation. If the value of this field changes, the continuous summation starts again.

 

4. Obtain the previous or next row of the current data.

Lead (field) to get the next row of the current data

Lag (field) obtains the previous row of the current data

These two functions are also used together with the over function.

Select ename, Sal, lead (SAL) over (order by SAL) Aaa, lag (SAL) over (order by SAL) BBB from EMP;

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.