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)
 Update - Self Join and Group By
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Brittney10
Posting Yak Master

USA
152 Posts

Posted - 12/28/2012 :  11:43:29  Show Profile  Reply with Quote
Below is an example record set:

ID_____Date________Value____Assoc
123____12/28/2012____0_______1
123____12/27/2012____0_______1
123____12/26/2012____0_______2
123____12/25/2012____0_______2

What I need to be able to do is update [Value] to 2 when [Date] is the most recent per [Assoc]. So in the above query, record #1 and record #3 would both be updated. (Grouping by ID and Assoc)

Desired Result set:
ID_____Date________Value____Assoc
123____12/28/2012____2_______1
123____12/27/2012____0_______1
123____12/26/2012____2_______2
123____12/25/2012____0_______2

Thanks for the help ahead of time.

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/28/2012 :  11:54:53  Show Profile  Reply with Quote
Update T
Set T.Value = Isnull(2,0)
from
(
Select *,DENSE_RANK() OVER(PARTITION BY ID,Assoc Order by Date desc) as Seq
from Table
)T
Where T.Seq = 1
Go to Top of Page

Brittney10
Posting Yak Master

USA
152 Posts

Posted - 12/28/2012 :  12:33:12  Show Profile  Reply with Quote
That works great! Thank you. But now I have another problem. I have duplicate records, so I need to update the one who has the most recent identity value. How do I add that logic?
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/28/2012 :  12:47:22  Show Profile  Reply with Quote
Update T
Set T.Value = 2
from
(
Select *,DENSE_RANK() OVER(PARTITION BY ID Order by Date desc) as Seq
from Table
)T
Where T.Seq = 1
Go to Top of Page

Brittney10
Posting Yak Master

USA
152 Posts

Posted - 12/28/2012 :  15:01:00  Show Profile  Reply with Quote
That doesn't quite work because I still need to partition by ID and Assoc.


quote:
Originally posted by sodeep

Update T
Set T.Value = 2
from
(
Select *,DENSE_RANK() OVER(PARTITION BY ID Order by Date desc) as Seq
from Table
)T
Where T.Seq = 1


Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/28/2012 :  15:18:46  Show Profile  Reply with Quote
Do you have Primary key with identity? or explain what you mean by this "I need to update the one who has the most recent identity value"
Go to Top of Page

Brittney10
Posting Yak Master

USA
152 Posts

Posted - 12/28/2012 :  16:08:25  Show Profile  Reply with Quote

Identity____ID_____Date________Value____Assoc
1___________123____12/28/2012____0_______1
2___________123____12/28/2012____0_______1
3___________123____12/27/2012____0_______2
4___________123____12/27/2012____0_______2

Since I have duplicate records, I need to update the record with the most recent Identity (auto incrementing) column. So record #2 and #4 would be updated. The code you gave me works perfect, except [Value] can only be = 2 once per ID and Assoc. I hope that makes more sense. Thanks for the help!
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/28/2012 :  16:18:52  Show Profile  Reply with Quote
Ok then do this

Update T
Set T.Value = 2
from
(
Select *,DENSE_RANK() OVER(PARTITION BY ID Order by Date desc,<Your Identity Column> desc) as Seq
from Table
)T
Where T.Seq = 1
Go to Top of Page

Brittney10
Posting Yak Master

USA
152 Posts

Posted - 12/28/2012 :  16:20:56  Show Profile  Reply with Quote
Perfect! I swear I tried that! Perhaps I was doing "Order By Date, Identity desc". Thank you so much for the help!

quote:
Originally posted by sodeep

Ok then do this

Update T
Set T.Value = 2
from
(
Select *,DENSE_RANK() OVER(PARTITION BY ID Order by Date desc,<Your Identity Column> desc) as Seq
from Table
)T
Where T.Seq = 1


Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/28/2012 :  16:21:43  Show Profile  Reply with Quote
NP. Atleast you learnt now :)
Go to Top of Page

Brittney10
Posting Yak Master

USA
152 Posts

Posted - 12/28/2012 :  16:39:22  Show Profile  Reply with Quote
I did learn a lot! Thanks for that!

quote:
Originally posted by sodeep

NP. Atleast you learnt now :)

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