loading..
Ðóññêèé    English
14:11

ROW_NUMBER function page 2

MySQL has not ranking/window functions in its staff, but you can use variables in a SQL query directly. Particularly, the problem of numbering query result rows can be solved with using variables. Let's demonstrate it by example being considered on the previous page.

  1. SELECT @i:=@i+1 num,
  2.      trip_no, id_comp
  3.     FROM Trip, (SELECT @i:=0) X
  4.     WHERE ID_comp < 3
  5.     ORDER BY id_comp, trip_no;

num     id_comp     trip_no
1     1     1181
2     1     1182
3     1     1187
4     1     1188
5     1     1195
6     1     1196
7     2     1145
8     2     1146

In the third line of the script, initializing of variable is accomplished. As a result, each row of Trip table will be joined the single-column row containing 0 (just Cartesian product).

In the first line of the script, variable is incrementing by 1, and this action takes place when calculating each next row in the sorting that is specified in ORDER BY clause. The numbering in this case corresponds to the sorting order.

If you omit the initialization of variable, you might obtain the correct result but without garantee. If you run the query (without initializing) once more in the current session of connection with database, you should obtain numbering which is starting from the maximal value of variable @i reached at the previous execution of the query.

We can get numbering of rows for each company separately, i.e. to imitate the behaviour of PARTITION BY in the query:

Console
Execute
  1.     SELECT row_number() over(PARTITION BY id_comp ORDER BY id_comp,trip_no) num,
  2.      trip_no, id_comp
  3.     FROM Trip
  4.     WHERE ID_comp < 3
  5.     ORDER BY ID_comp, trip_no;

 Let's introduce another variable for storing the company ID (@comp). When initializing variable, we assign unexisting ID (0, for example). Then for each row, the value of variable is comparing with company ID of the current row. If these values equal each other, @i is incrementing; if not - @i is resetting to 1. Finally, we assign the company ID from current row to variable @comp.  The matter is that the comparison takes place before assigning, in so doing we compare the current value of company ID with the ID from previous row (in the given sorting order). Here the query is.

  1. SELECT
  2. CASE WHEN @comp=id_comp THEN @i:=@i+1 ELSE @i:=1 END num,
  3. @comp:=id_comp id_comp, trip_no
  4.     FROM Trip, (SELECT @i:=0, @comp:=0) X
  5.     WHERE ID_comp < 3
  6.     ORDER BY ID_comp, trip_no;

num     id_comp     trip_no
1     1     1181
2     1     1182
3     1     1187
4     1     1188
5     1     1195
6     1     1196
1     2     1145
2     2     1146

As we don't follow  SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Standard here, let's use MySQL IF function to make the query shorter.

  1. SELECT
  2. IF(@comp=id_comp, @i:=@i+1, @i:=1) num,
  3. @comp:=id_comp id_comp, trip_no
  4.     FROM Trip, (SELECT @i:=0, @comp:=0) X
  5.     WHERE ID_comp < 3
  6.     ORDER BY id_comp, trip_no;

To check the above queries, use console when selecting MySQL as target DBMS.

Pages 1 2
Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
several days ago
https://exchangesumo.com/obmen/SEPAUSD-KUKRUB/
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.