SQL clever but useless (01): Sort data to compile a number of isolated [row_number () over (order by c)] (mysql, db2, oracle, sqlserver generic)

Original: SQL clever but useless (01): Sort data to compile a number of isolated [row_number () over (order by c ) ] (mysql, db2, oracle, sqlserver generic)

Every day we are dealing with a database, wrote countless code, written SQL is a few laps around the earth. Collected here are magical about SQL grammar and usage, although you may not have used, but these SQL but they can at a critical time, come in handy.

My understanding of SQL statements, can be compared to a bridge, the fragmented data combined to get valid information I need. Also record it using this experience

A. Basic grammar and usage

Note: ROW_NUMBER () can not be used alone, and requires over (order by col) used together.

Syntax 1:

row_number() over(ORDER BY col)

Meaning: Briefly ROW_NUMBER () starting from 1, it returns a record number for each packet, where the ROW_NUMBER () OVER (ORDER BY xlh DESC) is the first column in descending XLH, then after descending to return records each XLH a serial number.

Syntax 2:

row_number() over(PARTITION BY col1 ORDER BY col2)

Meaning: shows a packet col1, col2 ordered within a packet according to, and this function represents the value calculated on the sequence number after each internal order (unique within the set of consecutive)

About Parttion by:

Parttion by SQL keywords are part of the analytical function for the result set to be partitioned. It polymerization Group by a different function is that it simply ranking the original data arrangement, a plurality of records can be returned (the same number of records) of a packet, and the original data is a Group by statistic polymerization, generally reflect only one result of the statistical value (a return each).

If you want to press back Parttion by a plurality of packet fields, separated by a comma.

row_number()  over(PARTITION BY col1,col3,col4 ORDER BY col2)

Example:

(1) made to build the table data:

create table employee (empid int ,deptid int ,salary decimal(10,2));
insert into employee values(1,10,5500.00);
insert into employee values(2,10,4500.00);
insert into employee values(3,20,1900.00);
insert into employee values(4,20,4800.00);
insert into employee values(5,40,6500.00);
insert into employee values(6,40,14500.00);
insert into employee values(7,40,44500.00);
insert into employee values(8,50,6500.00);
insert into employee values(9,50,7500.00);

Data are shown as:

(2) demand: according to sector groups, showing the wage scale for each department

sql:

SELECT  *, 
      row_number() over(PARTITION by deptid ORDER BY salary desc)  as score 
 FROM 
      employee

expected results:

II. Real Stories

By the above basic grammar and usage, we simply know row_number () can be used to sort the use of numbers, and packet ordering within two scenarios. Through an understanding of their after, your heart is full of doubt, if for the project, we can take it what to do, but also what the problem is solved.

This syntax is also my recent work content when we come into contact with the previously only vaguely aware of the stay, but the job is not actually used it.

Described above is operated in a single table, the table for a slightly more complex if there are considered related operation

Here's an example of close around to learn about SQL Usage:

Left connecting a plurality of data tables may meet the situation, but only taking the first condition is satisfied, i.e., taken from the query data associated max

select 
      a.*, b.* 
from 
    girl g, 
    (select *, row_number() over(ORDER BY like desc) as rn from boy) b 
where b.rn = 1 and g.like = b.like

Above mean: girl and boy table associated with the table, the boy let the girl to find the favorite; boy table is sorted according to the preference value.

So you can spend it.

III. Summary

Selection clause must be used over a column of row_number () is very versatile, it is best to use the sort, it will generate a query sequence number out of each row are sequentially sorted and does not repeat, when attention function using row_number in order to generate sorting number.

Also, when using a ranking function to note the following three points:

  • 1 ranking function must have the OVER clause.

  • 2, the ranking function must have the ORDER BY clause contains OVER.

  • 3, from the start ordering within a packet.

Multi-database compatibility this method is good if you want to deal with the database, it can also make a good collection.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11432673.html