SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Remove duplicate values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

infodemers
Posting Yak Master

Canada
181 Posts

Posted - 03/21/2013 :  13:40:53  Show Profile  Reply with Quote
Hi,

I have to do some clean up on a regular basis on a table because of the bad architecture of the DB.
I want to assign the value for the Remark field to the item with the newest effective date only. So in the following example the value for the remark would be blank or null in second row.

Cust|Name|EffectiveDate|Remark|
514-371|MTRL|2013-03-21|CHG POI|
514-371|MTRL|2001-10-04|CHG POI|

Thanks!

yosiasz
Flowing Fount of Yak Knowledge

USA
1634 Posts

Posted - 03/21/2013 :  14:04:14  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
create table #booya (Cust varchar(50), name varchar(50), EffectiveDate datetime, Remark varchar(50))
insert into #booya
select '514-371', 'MTRL','2013-03-21','CHG POI'

union
select '514-371','MTRL','2001-10-04','CHG POI'

select Cust, EffectiveDate,
RANK() OVER
(PARTITION BY Cust ORDER BY EffectiveDate DESC) AS ranktology
from #booya

;with cteMango(Cust, EffectiveDate, ranktology)
as
(
select Cust, EffectiveDate,
RANK() OVER
(PARTITION BY Cust ORDER BY EffectiveDate DESC) AS ranktology
from #booya
)
update tgt
set tgt.Remark = case when src.ranktology <> 1 then null
else tgt.Remark
end
from cteMango src
join #booya tgt
on src.Cust = tgt.Cust

select * from #booya

Edited by - yosiasz on 03/21/2013 14:07:28
Go to Top of Page

infodemers
Posting Yak Master

Canada
181 Posts

Posted - 03/21/2013 :  16:22:34  Show Profile  Reply with Quote
Hi Yosiasz,

Thanks for the snippet. I could make it using some pieces of your code.
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1634 Posts

Posted - 03/21/2013 :  17:05:29  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
sure thing.

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000