Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
anjali5
Posting Yak Master
121 Posts |
Posted - 2011-11-11 : 15:57:03
|
| I have the following tableID UID number Initial_date close_date update3511 21346 201005 2010-06-0723:35:002010-06-0723:35:00 0 25202 21346 201005 2009-09-0723:35:002011-06-0923:53:35:00 123652 76543 567433 2005-08-0823:43:00 2005-23--07 23:45:00 123653 76543 567433 2011-9-0923:45:00 2012-23-09 23:56:00 0I want to update the initial_date with the earliest date and close_date with the latest date fo a given UID and number. so as long as UID and numberare same, I need to update both initial_date and close date so in the above example, I want to update the Initial_date for UID 21346 and number 201005 to 2010-06-0723:35:00 and close_date to 2011-06-0923:53:35:00and for UID 76543 and number 567433 , I need to update initial date to 2005-08-0823:43:00 and close_date to 2012-23-09 23:56:00.the UID and number are not in order. How can I acheive thisThanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-12 : 05:27:57
|
| [code]UPDATE tSET t.Initial_date = t1.MInInitial,t.close_date = t1.MaxCloseFROM table tINNER JOIN (SELECT UID,number,MIN(Initial_date) AS MInInitial,MAX(close_date) AS MaxClose FROM table GROUP BY UID,number )t1ON t1.UID= t.UIDAND t1.number = t.number[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|