Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

1
  • select UID, EventID, Status, sum(case when lagstatus = Status then 0 else 1 end) as status_counter INTO #tbl1 from (select t.*, lag(Status) over (partition by UID order by EventID) as lagstatus from TABLE_MAIN t ) t GROUP BY UID,EventID, Status select UID, EventID ,ROW_NUMBER() OVER(PARTITION BY UID ORDER BY EventID) as COUNTER into #FinalTable2 from #FINALTABLE1 where status_counter=1 Select f1.*,COUNTER from TABLE_MAIN f1 left join #tbl1 f2 ON f1.UID= f2.UID and f1.EventID=f2.EventID I did the above steps &Then I copied the Counter to other null values which were resulted by left join Commented Sep 17, 2015 at 4:27