guinnie
asked on
Updating row with most current date
I have a table with a customer numbe, order numberr , 'current indicator' and a 'create date'.
There will be multiple rows with same customer number, current indicator' and order number.
The only difference will be create date.
I want to update current_indicator with a 'Y' on row with most current date.
Can someone do this using max function on the date column ?
There will be multiple rows with same customer number, current indicator' and order number.
The only difference will be create date.
I want to update current_indicator with a 'Y' on row with most current date.
Can someone do this using max function on the date column ?
ASKER
That didn't work. No rows returned.
Ok , i tried to simplify request but I'll give you more info.
Here is my data. First column is student_skey. Second column is create_date.
127 2010-09-25 04:08:14.000
127 2010-09-25 06:19:45.000
127 2010-10-11 23:28:29.000
127 2010-10-12 02:18:30.000
127 2010-10-16 19:26:50.000
Here is my code based on your response:
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by create_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator <> 'N'
Ok , i tried to simplify request but I'll give you more info.
Here is my data. First column is student_skey. Second column is create_date.
127 2010-09-25 04:08:14.000
127 2010-09-25 06:19:45.000
127 2010-10-11 23:28:29.000
127 2010-10-12 02:18:30.000
127 2010-10-16 19:26:50.000
Here is my code based on your response:
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by create_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator <> 'N'
try this.
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by create_date desc) as rn
from @table) as U
where rn=1
and ISNULL(current_indicator,'N') <> 'Y'
you want to change rows where the current indicator isn't 'Y' to 'Y'
if the rows currently contain an 'N' then you want this.....
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by create_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator = 'N'
if the rows currently contain an 'N' then you want this.....
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by create_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator = 'N'
ASKER
The point is I only want to update the row with the most current date.
Not all rows.
Not all rows.
both my and sharaths solution shoulld only be updating the student row with the latest date per student...
127 2010-09-25 04:08:14.000
127 2010-09-25 06:19:45.000
127 2010-10-11 23:28:29.000
127 2010-10-12 02:18:30.000
127 2010-10-16 19:26:50.000
so we should only affect the 2010-10-16 row for student 127,,,,
what are we missing can you provide an example with multiple students/dates and highklight what you desire to be updated...
(include the current indicator , since i'm getting confused as to what its state ie/should be.....)
127 2010-09-25 04:08:14.000
127 2010-09-25 06:19:45.000
127 2010-10-11 23:28:29.000
127 2010-10-12 02:18:30.000
127 2010-10-16 19:26:50.000
so we should only affect the 2010-10-16 row for student 127,,,,
what are we missing can you provide an example with multiple students/dates and highklight what you desire to be updated...
(include the current indicator , since i'm getting confused as to what its state ie/should be.....)
ASKER
When I run the query below , the result says '0 rows affected'.
I need the current indicator set to 'Y' on the most current row.
Herre is the sql is used:
go
begin transaction
go
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by cre_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator = 'N'
and student_skey = '127'
and SCHOOL_YEAR_code = '20102011'
go
I need the current indicator set to 'Y' on the most current row.
Herre is the sql is used:
go
begin transaction
go
update U
set current_indicator='Y'
from (select *, row_number() over
(partition by student_skey order by cre_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator = 'N'
and student_skey = '127'
and SCHOOL_YEAR_code = '20102011'
go
ASKER
LowFatSpread or Sharath:
Can you see anything in SQL that was not correct ?
Can you see anything in SQL that was not correct ?
Can you check the current_indecator value with a SELECT query for that record?
select *
from (select *, row_number() over
(partition by student_skey order by cre_date desc) as rn
from fa_school_enrollment) as U
where rn=1
and current_indicator = 'N'
and student_skey = '127'
and SCHOOL_YEAR_code = '20102011'
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Sharath:
Thanks for the quick reply.
I took the filters and put them in the 'partition' statement
and reran update. Then it worked perfectly. See below.
Thanks for working with me on this.
update U
set current_indicator='Y'
from (select *
,row_number() over (partition by student_skey order by cre_date desc) as rn
from fa_school_enrollment
where student_skey = '127'
and SCHOOL_YEAR_code = '20102011') as U
where rn=1
Thanks for the quick reply.
I took the filters and put them in the 'partition' statement
and reran update. Then it worked perfectly. See below.
Thanks for working with me on this.
update U
set current_indicator='Y'
from (select *
,row_number() over (partition by student_skey order by cre_date desc) as rn
from fa_school_enrollment
where student_skey = '127'
and SCHOOL_YEAR_code = '20102011') as U
where rn=1
you meant Lfs?
set current_indicator='Y'
from (select *
,row_number() over (partition by customer_number,order_numb
from yourtable) as U
where rn=1
and current_indicator <>'Y'