Question : Sql query returning different results each time it is run even though conditions are the same.

I have the following query which works.

SELECT     count(*)
FROM         (SELECT     EntSys,School_code,school_name,ADMISSION_DATE,HP_Type,HP_CODE,HP_Name, ROW_NUMBER() OVER (PARTITION BY EntSys
                       ORDER BY ADMISSION_DATE desc) rn
FROM         [All Schools Excluding Nurseries]) sq
WHERE     sq.RN = 1

However when I add more WHERE conditions it become unstable and does not return a consistent result.

For example:


SELECT     count(*)
FROM         (SELECT     EntSys,School_code,school_name,ADMISSION_DATE,HP_Type,HP_CODE,HP_Name, ROW_NUMBER() OVER (PARTITION BY EntSys
                       ORDER BY ADMISSION_DATE desc) rn
FROM         [All Schools Excluding Nurseries]) sq
WHERE     sq.RN = 1 and  HP_Type='SN'

This will return a count of 64429 the first time it is run.

The second time it runs the count = 64433
The forth time it runs the count = 64644  
and so on... it is not stable.

Can anybody think of a reason why the extra WHERE condition causes such a problem.

Answer : Sql query returning different results each time it is run even though conditions are the same.

sory for my mistake this is the correct one
1:
2:
3:
4:
5:
SELECT *
FROM   (SELECT     EntSys,School_code,school_name,ADMISSION_DATE,HP_Type,HP_CODE,HP_Name, ROW_NUMBER() OVER (PARTITION BY EntSys ORDER BY ADMISSION_DATE desc) rn
		FROM         [All Schools Excluding Nurseries]
		WHERE HP_Type='SN' AND entsys = 003 ) sq
WHERE     sq.RN = 1
Random Solutions  
 
programming4us programming4us