(The answer is given before the OP indicated in 2008. The following works only in 2012)
This query gives (for each ID value) the number of rows in the last 10 for which the flag is 1. It should be simple enough (if required) to filter it only for rows for which the counter is 10 and limit it to one ID value .
Without the best sample data, I will leave this for now:
;with Vals as ( select *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY CreatedDate DESC) as rn, SUM(CASE WHEN Flag = 1 THEN 1 ELSE 0 END) OVER (PARTITION BY ID ORDER BY CreatedDate ASC ROWS BETWEEN 9 PRECEDING AND CURRENT ROW) as Cnt from T1 ) select * from Vals where rn = 1
(It depends on the version of SQL Server 2012 OVER clause - but you did not specify which version)
Result:
ID Flag CreatedDate rn Cnt ----------- ----- ----------------------- -------------------- ----------- 1 1 2012-01-12 00:00:00.000 1 10 2 1 2012-01-12 00:00:00.000 1 9 3 1 2012-01-12 00:00:00.000 1 6
(Only ID 1 matches your criteria)
Sample data:
create table T1 (ID int not null,Flag bit not null,CreatedDate datetime not null) insert into T1 (ID,Flag,CreatedDate) values (1,1,'20120101'), (1,0,'20120102'), (1,1,'20120103'), (1,1,'20120104'), (1,1,'20120105'), (1,1,'20120106'), (1,1,'20120107'), (1,1,'20120108'), (1,1,'20120109'), (1,1,'20120110'), (1,1,'20120111'), (1,1,'20120112'), (2,1,'20120101'), (2,1,'20120102'), (2,1,'20120103'), (2,1,'20120104'), (2,1,'20120105'), (2,1,'20120106'), (2,0,'20120107'), (2,1,'20120108'), (2,1,'20120109'), (2,1,'20120110'), (2,1,'20120111'), (2,1,'20120112'), (3,1,'20120107'), (3,1,'20120108'), (3,1,'20120109'), (3,1,'20120110'), (3,1,'20120111'), (3,1,'20120112')