Database SQL uses lag and over functions and like use tips

Source: Internet
Author: User

1.

Sample 1:

Lag () is the previous row of records in the current order. The combination of over is the grouping statistical data.
The Lag () function is the data that goes to the field on the N row.

Sql> select * from X;

A
----------
1
2
3
5

Sql> Select a as snaped,
Lag (A, 2) over (ORDER by a) as Snapst
from X;

snaped Snapst
---------- ----------
1
2
3 1
5 2

Sql>

Sample 2:

Sql> select * from test;

QTY
----------
1
2
3
4

sql> Select SUM (qty) from test;

SUM (QTY)
----------
10

sql> Select SUM (qty) over (Order by qty) from test;

SUM (QTY) over (Orderbyqty)
------------------------
1
3
6
10

2.sql wildcard characters

Wildcard Description Example
% any string that contains 0 or more characters. The WHERE title like '%computer% ' will find all titles that contain the word computer anywhere in the title.
_ (underline) any single character. WHERE au_fname like ' _ean ' will look for all 4-letter names (Dean, Sean, etc.) ending in EAN.
[] Specifies any single character in the range ([A-f]) or collection ([abcdef]). WHERE au_lname like ' [C-p]arsen ' will find the last name of the author ending with Arsen and starting with any single character between C and P, for example, Carsen, Larsen, Karsen, and so on.

[^] Any single character that does not belong to the specified range ([a-f]) or set ([abcdef]). WHERE au_lname like ' de[^l]% ' will look for the last names of all authors that begin with the de and whose letters are not followed by L.

Database SQL uses lag and over functions and like use tips

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.