SQLServerCentral.com / Article Discussions / Article Discussions by Author / Discuss content posted by zulmanclock / Sequence / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comTue, 03 Mar 2015 17:59:36 GMT20RE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspx[quote] get all those numbers in one row [/quote]Now I understand what you were trying to achieve. Thanks for the question.Fri, 29 Apr 2011 09:59:12 GMTterrykzncsRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspx[quote][b]SQLkiwi (12/28/2010)[/b][hr]Or, even more succinctly:[code="sql"]WITH Records (Old_Seq, New_Seq)AS ( SELECT Seq, ROW_NUMBER() OVER (PARTITION BY Value ORDER BY RecID) FROM #Test )UPDATE RecordsSET Old_Seq = New_Seq;[/code]Paul[/quote]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.OlegTue, 28 Dec 2010 13:35:44 GMTOleg NetchaevRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspx[quote][b]Oleg Netchaev (12/21/2010)[/b][hr]For example, the update in question can be easily restated like this:[/quote]Or, even more succinctly:[code="sql"]WITH Records (Old_Seq, New_Seq)AS ( SELECT Seq, ROW_NUMBER() OVER (PARTITION BY Value ORDER BY RecID) FROM #Test )UPDATE RecordsSET Old_Seq = New_Seq;[/code]PaulTue, 28 Dec 2010 13:03:21 GMTPaul WhiteRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxThanks for the question.Tue, 28 Dec 2010 10:52:23 GMTUMG DeveloperRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxThank 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 exampleaccoount field1234 123456781234 901234561234 78901234the 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 row1234 12345678 90123456 78901234using 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.Fri, 24 Dec 2010 18:25:58 GMTzulmanclockRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxThanks for the questionWed, 22 Dec 2010 16:16:51 GMTSQLRNNRRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspx[quote][b]da-zero (12/22/2010)[/b][hr][quote][b]paul.goldstraw (12/22/2010)[/b][hr]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?Paul[/quote]Unless you implement the update as an delete and an insert and something goes horribly wrong in between, I would be very surprised :-)[/quote]That's what I thought :) Thanks for the confirmationPaulWed, 22 Dec 2010 08:28:54 GMTpaul.goldstrawRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspx[quote][b]paul.goldstraw (12/22/2010)[/b][hr]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?Paul[/quote]Unless you implement the update as an delete and an insert and something goes horribly wrong in between, I would be very surprised :-)Wed, 22 Dec 2010 08:13:52 GMTKoen VerbeeckRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxI 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?PaulWed, 22 Dec 2010 07:14:56 GMTpaul.goldstrawRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspx[quote][b]Toreador (12/22/2010)[/b][hr]I get a pile of errors when I run it - Cannot insert the value NULL into column 'Seq'.I eventually worked out that this is because I have ANSI_NULL_DFLT_ON set to False, so Seq was added as a Not Null column.This never normally affects me, as I always specify NULL or NOT NULL and don't rely on a default, but can anyone point me in the direction of where this might be set?[/quote]Right-click on the database in SSMS, select properties and in the properties editor select the options tab. There you can set the value for 'ANSI NULL default'. You can also use the sp_dboption procedure.Wed, 22 Dec 2010 05:16:33 GMTKoen VerbeeckRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxGood question.Wed, 22 Dec 2010 04:06:14 GMTjaganmohan.raoRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxI get a pile of errors when I run it - Cannot insert the value NULL into column 'Seq'.I eventually worked out that this is because I have ANSI_NULL_DFLT_ON set to False, so Seq was added as a Not Null column.This never normally affects me, as I always specify NULL or NOT NULL and don't rely on a default, but can anyone point me in the direction of where this might be set?Wed, 22 Dec 2010 03:59:21 GMTToreadorRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxThe question itself was OK, as a test to see what output the T-SQL would give.But not OK for the purpose mentioned in the explanation, as there are other statements that will do it much better and faster (as Oleg has already mentioned).Wed, 22 Dec 2010 02:17:01 GMTKoen VerbeeckRE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxNice question, thanks also Oleg for the remark and link to Triangular Joins.Regards,IulianWed, 22 Dec 2010 01:08:32 GMTIulian -207023RE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxGood question. Thanks 'Oleg Netchaev' for query & link.Tue, 21 Dec 2010 22:48:27 GMTHardy21RE: Sequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxThis is a good basic question, thank you. One thing I would like to point out is that the update script in question is more complex than it needs to be and it uses what Jeff Moden calls a triangular join, which is one of the most evil things one can do to kill performance of the T-SQL query. Of course back in the dark ages when we had to work with SQL Server 2000, the triangular joins were a necessary evil sometimes, but in nowadays when the end of support for SQL Server 2000 is long as ended and the end of support for SQL Server 2005 is looming (12 of April 2011), it is about time to stop the triangular joins insanity and take a look at the windowing functions instead. For example, the update in question can be easily restated like this:[code="sql"];with records (RecID, Seq) as( select RecID, row_number() over(partition by Value order by RecID) from #Test) update #Test set Seq = records.Seq from #Test inner join records on #Test.RecID = records.RecID;[/code]Please, please read Jeff's article: [url=http://www.sqlservercentral.com/articles/T-SQL/61539/]http://www.sqlservercentral.com/articles/T-SQL/61539/[/url]It is a true eye opener!Just my 2 cents.OlegTue, 21 Dec 2010 21:26:53 GMTOleg NetchaevSequencehttp://www.sqlservercentral.com/Forums/Topic1038039-2875-1.aspxComments posted to this topic are about the item [B]<A HREF="/questions/T-SQL/71890/">Sequence</A>[/B]Tue, 21 Dec 2010 20:30:29 GMTzulmanclock