Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

inconsistent record set using ROW_NUMBER() function Expand / Collapse
Author
Message
Posted Thursday, January 29, 2009 8:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 1, 2014 10:58 AM
Points: 1, Visits: 171
Getting inconsistent record set While doing paging using ROW_NUMBER() function. Each time of execution i am getting different 100th record.after going throw the actual records, i found order by clause column fetches random records when it has same records in last record in current page and first record in next page.Below are my sample data.

100 17789 Ramesh Patel Queens 2007-08-31 17:08:03.650 242951 Placement 2006-08-01 00:00:00.000 (ACC) McDonalds
101 42642 John Kim Queens 2007-09-10 18:05:18.977 244446 Placement 2007-06-01 00:00:00.000 (ACC) McDonalds
102 250977 Roget Sachin Queens 2007-08-29 16:38:04.557 242200 Placement 2007-04-24 00:00:00.000 (ACC) McDonalds
103 277414 Fed Khan Queens 2008-04-21 18:17:59.930 296856 Placement 2007-12-02 00:00:00.000 (ACC) McDonalds
104 285158 Rodger mo Queens 2008-06-26 14:31:10.970 315119 Placement 2008-01-04 00:00:00.000 (ACC) McDONALDS
105 158322 Andy Thomas Queens 2008-04-11 16:20:15.540 294820 Placement 2007-11-19 00:00:00.000 (ACC) McDonalds

Since order by column has same value as "(ACC) McDonalds" and displaying 100 records per page,so each time of 100th record is coming randomly from those 6 records.

Query:-

SELECT RowNum, PeopleID, FIRSTNAME,LASTNAME,Center ,DateCreated ,WorkHistID,Source,JobStartDate,Employer,Position,WageHour
FROM
(SELECT P.PeopleID, P.FIRSTNAME,P.LASTNAME
,C.CenterNameShort as Center
,WH.DATECREATED as DateCreated ,WH.WorkHistID,WH.Source, WH.JobStartDate,O.ORGANIZATIONNAME AS Employer,
WH.JobTitle as Position,WH.WAGETOTALCOMPENSATION as WageHour
,ROW_NUMBER() OVER (ORDER BY O.ORGANIZATIONNAME ASC) as RowNum
from PEOPLE P
INNER JOIN Workhist WH ON(P.PeopleID = WH.PeopleID)
INNER JOIN CenterMaster C ON(WH.CenterID =C.CenterID)
INNER JOIN OrganizationMaster O ON(WH.OrganizationID=O.OrganizationID)
where C.CenternameShort= @CenterID
and WH.datecreated >= @StartDate AND WH.datecreated < @EndDate + 1 AND WH.Source= @Source
) AS Page
WHERE Page.RowNum BETWEEN (@PageIndex * @PageSize+ 1) and ((@PageIndex + 1) * @PageSize)



Below are Sample Records:-

100 17789 Ramesh Patel Queens 2007-08-31 17:08:03.650 242951 Placement 2006-08-01 00:00:00.000 (ACC) McDonalds
101 42642 John Kim Queens 2007-09-10 18:05:18.977 244446 Placement 2007-06-01 00:00:00.000 (ACC) McDonalds
102 250977 Roget Sachin Queens 2007-08-29 16:38:04.557 242200 Placement 2007-04-24 00:00:00.000 (ACC) McDonalds
103 277414 Fed Khan Queens 2008-04-21 18:17:59.930 296856 Placement 2007-12-02 00:00:00.000 (ACC) McDonalds
104 285158 Rodger mo Queens 2008-06-26 14:31:10.970 315119 Placement 2008-01-04 00:00:00.000 (ACC) McDONALDS
105 158322 Andy Thomas Queens 2008-04-11 16:20:15.540 294820 Placement 2007-11-19 00:00:00.000 (ACC) McDonalds

Getting inconsistent record set While doing paging using ROW_NUMBER() function.

Is there any issue in my query or there is a issue in the behaviour of ROW_NUMBER() function???

Any help or suggestion will be appreciated.

Thanks
Post #646012
Posted Thursday, December 10, 2009 3:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, August 22, 2014 4:18 AM
Points: 2,048, Visits: 1,403
Hello,

You've probably long since found the answer to your question given that your post is almost 1 year old, but it's because the ROW_NUMBER() functions is non-deterministic unless you provide a 'tie-breaker' to make the values unique. In your example you could use:
ROW_NUMBER() OVER (ORDER BY O.ORGANIZATIONNAME, P.PeopleID) AS Rownum

Regards
Lempster
Post #832094
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse