SQL combines multiple query results with the sequence number of ROW_NUMBER for the results of the query

  • 2020-05-07 19:33:30
  • OfStack

The original author seems to be called topcat, I understand 1 by myself and write it down for backup from memory.
If Select Name From StudentTable is used, the result is:
Name
3
4 li
If you use Select ROW_NUMBER() AS ROWINDEX, Name From StudentTable, the result is shown
ROWINDEX Name
1 3
2 li 4
Of course, there is no need to add Numbers to the results of a 1 query, because the list itself is numbered when the data is displayed. But the advantage of this is that you can query multiple result sets in the database and then link them back to the customer after 1, which is more flexible.
In addition, there are several parameters to choose from:
1. ROW_NUMBER ()
For example: Select ROW_NUMBER() OVER(ORDER BY Name) AS ROWINDEX, Name From StudentTable
The effect is to sort in order
2. RANK ()
For example: Select RANK() OVER(ORDER BY Name) AS ROWINDEX, Name From StudentTable
The effect is that if there are 5 people called 3, then 5 3 are 1, then the first li 4 is 6, and so on
3. DENSE_RANK ()
It's similar to the last one, except that the first lee 4 is number 2


Related articles: