Frage

Good Evening, I currently study the ROW_NUMBER() in squirrel sql. I success to retrieve data from the database, but the problem is the R in the ROW_NUMBER() OVER () AS R does not match with the sorted database data. Need some suggestion and hints for the query below, thanks

Query

SELECT COUNT(*) AS RECORDS  FROM (SELECT ROW_NUMBER() OVER () AS R,
somethingAisID, somethingB, somethingC ORDER BY somethingA ) AS T WHERE R>=0
AND R<=100

p/s: The database rows is sorted, but the R fields is match with the un-sorted database rows.

Example:

the somethingAisID which sorted from the query is 1,2,3,4,5

the somethingAisID which un-sorted from the original database table is 4,5,2,3,1

then the R is match as 4,5,2,3,1

How can I query the R to match as 1,2,3,4,5? (sorted query)

War es hilfreich?

Lösung

You have to use ORDER BY in OVER clause for that

ROW_NUMBER() OVER (ORDER BY column_name [ASC|DESC]) AS R

Here is SQLFiddle demo

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top