|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 3:38 AM
Points: 812,
Visits: 1,134
|
|
da-zero (12/22/2010)
paul.goldstraw (12/22/2010) I got it right based on the fact that an update statement shouldn't ever be able to change the number of records in a table - since 8 rows were inserted the only possible answer was the first one (assuming there was no error). I got it right but was I correct in making that assumption or did I just get lucky? I can't think of any circumstance where an update would do that but have I missed something?
PaulUnless you implement the update as an delete and an insert and something goes horribly wrong in between, I would be very surprised 
That's what I thought :) Thanks for the confirmation
Paul
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Yesterday @ 9:24 AM
Points: 207,
Visits: 218
|
|
Thank you all for your comments. LIttle history behind why i came up with this querey. I work as a sql dba and report writer for a legal collections agency. we have tables were there are multiple rows for each account something to the nature of the below example
accoount field 1234 12345678 1234 90123456 1234 78901234
the field value is phone numbers or other data that was given to us by clients. what i was tasked to do was get all those numbers in one row
1234 12345678 90123456 78901234
using this statement i am able to get all instances of a sepcific account and line up the varios numbers given to us by clients. i am going to try the suggestions i have seen here to change the query for more effeciency. what i am doing is loading the different instances of accounts into temp tables updating the sequence so that i can join in my final report. i am out of the office until tuesday but once back in office i will post my final query that i use for my report and possibly that will shed more light as to my madness :)
Again thank you all for comments.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:33 AM
Points: 10,989,
Visits: 10,529
|
|
Oleg Netchaev (12/21/2010) For example, the update in question can be easily restated like this: Or, even more succinctly:
WITH Records (Old_Seq, New_Seq) AS ( SELECT Seq, ROW_NUMBER() OVER (PARTITION BY Value ORDER BY RecID) FROM #Test ) UPDATE Records SET Old_Seq = New_Seq;
Paul
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 10:53 AM
Points: 1,662,
Visits: 1,709
|
|
SQLkiwi (12/28/2010)
Or, even more succinctly: WITH Records (Old_Seq, New_Seq) AS ( SELECT Seq, ROW_NUMBER() OVER (PARTITION BY Value ORDER BY RecID) FROM #Test ) UPDATE Records SET Old_Seq = New_Seq;
Paul This is really elegant, thank you so much Paul! Since there is no way to use the windowing functions directly in the set, I thought that update from join is a necessary evil to workaround the issue. I am glad that it is not.
Oleg
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Sunday, March 04, 2012 4:02 AM
Points: 660,
Visits: 134
|
|
get all those numbers in one row
Now I understand what you were trying to achieve. Thanks for the question.
|
|
|
|