I´m building a grid mechanism where I need to retrieve data from Database the total or records found, retrieving just a range of these records with a row_number in it.
I´m using SqlServer for testing, but I need to support that on Oracle and MySql as well.
That´s what I´m trying, but I can´t make it work:
SELECT * FROM
(SELECT ROW_NUMBER() AS RN,
COUNT(*) AS TOTALCN,
Id,
Name,
Phone
FROM MyTable WHERE Deleted='F')
WHERE RN > 100 AND RN < 150;
The idea is:
MyTable -> number of records: 1000
Select Id, Name, Phone from MyTable where Deleted='F' -> number of records: 850
Get the records 100 to 150 from the 850.
I want to get a select like:
RN TOTALCN Id Name Phone
1 850 Data Data Data
2 850 Data Data Data
3 850 Data Data Data
4 850 Data Data Data
5 850 Data Data Data
6 850 Data Data Data
The CN (850)
will be my grid total number of records.
The RN
will be grid index for data.
CAn someone help me to accomplish that ?
Thanks for any help.
[EDIT]
So, I will defenetely add an ORDER BY on every SELECT. Here is what I´m getting so far:
SELECT * FROM (SELECT ROW_NUMBER()
OVER (ORDER BY ID) AS RN,
COUNT(*) OVER (ORDER BY (SELECT NULL) AS CNT)
Id, Name Phone FROM MyTable WHERE Deleted='F')
T WHERE RN > 100 AND RN < 500;
Am I going to the right way ?
How expensive will be that to SQL ?