SQL Server - MyCTE query based on 24 hour period (next day) -


i have bit of code:

;with mycte (     select *,            row_number() over(partition carduser order cardtableid) newvariation       cardchecker ) update mycte  set    status = newvariation 

which updates status column, want happen on 24 hour period, status starts again next day @ 1, , counts again based on carduser specified above:

current data , happens:

2   aaa 1   2015-06-25 08:00:00.000 123 1   null 3   ccc 1   2015-06-25 00:00:00.000 124 1   null 4   aaa 1   2015-06-25 17:30:00.000 125 2   null 5   aaa 1   2015-06-26 17:30:00.000 125 *3* null 

what want happen:

2   aaa 1   2015-06-25 08:00:00.000 123 1   null 3   ccc 1   2015-06-25 00:00:00.000 124 1   null 4   aaa 1   2015-06-25 17:30:00.000 125 2   null 5   aaa 1   2015-06-26 17:30:00.000 125 *1* null 

im not quite sure how add above query possible point me in right direction?

the main problem eventtime field contains both date , time, adding partition means status 1 based on time parameter of field

thanks help

current cardtable structure:

create table cardtable (cardtableid int identity (1,1) not null, carduser varchar(50), cardnumber varchar(50), eventtime datetime, status int) 

you can convert() eventtime date type , partition:

;with mycte (     select status,            row_number() over(partition carduser, convert(date, eventtime)                               order cardtableid) newvariation       cardchecker ) update mycte  set    status = newvariation 

your query unnecessarily updating entire table everytime. if eventtime current date time of system, having flag mark updated status improve performance.

;with mycte (     select status,            row_number() over(partition carduser, convert(date, eventtime)                               order cardtableid) newvariation       cardchecker      status null or            convert(date, eventtime) = convert(date, getdate()) ) update mycte  set    status = newvariation  

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -