Question : Sql server qurey Issue

I have a  Table


DEipUserID      smallint      NOT NULL      PK
DEipServiceID      smallint      Y      PK
DEipServiceClientID      smallint      Y      
UseDateID      int      Y      
UseTimeID      int      Y      
SubscriptionProfileID      smallint      N      
IsLatest      Tinyint      Y      
UseCount      tinyint      Y      
DeipDataSourceID      int      N      
DEipTrackingID      bigint      N      
EipInsertDtTm      bigint      N      
EipUpdateDtTm      bigint      N      

How do I achieve  below logic..

For the first row islatest =1, 2nd row arrives go to prev record change IsLtatest = 0 and put IsLatest = 1 for new record.

Thanks In Advance

Answer : Sql server qurey Issue

>> the primary key is on (DEipUserID+DEipServiceID )
Ok, the query is incorrect then.  Because by definition, when you partition on the primary key, every single one is unique and will return row number of 1.

Going back to the question,
[For the first row islatest =1, 2nd row arrives go to prev record change IsLtatest = 0 and put IsLatest = 1 for new record.]
And one query you had above, I suspect you actually want, what is the definition of "2nd row", is it "2nd and thereafter rows per DEipUserID+DEipServiceClientID"?  Whatever it is, partition by that combination.


;with tmp as (
select deipuserid, deipserviceid, islatest, rn=row_number() over (partition by deipuserid, deipserviceClientid order by eipinsertdttm desc) from dbo.FactUserClientUse)
update tmp set islatest=case when rn=1 then 1 else 0 end
Random Solutions  
 
programming4us programming4us