﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Consolidating records - TSQL problem / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 13:58:12 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>I like the possibilities that rCTEs give us, but I also believe that sometimes the simplest way is the best way to go.I'm glad I could help.</description><pubDate>Wed, 26 Sep 2012 07:16:01 GMT</pubDate><dc:creator>Luis Cazares</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>I've messed around with this for a bit now and it's been a good learning exercise but I'm going to stick with the following (takes 3 seconds to work through 350K records)[code="sql"]declare @UpdatedRecords intset @UpdatedRecords = 1WHILE @UpdatedRecords &amp;gt; 0BEGIN	update b	set b.retained=a.retained	from testing1 as a	inner join testing1 as b	on b.retained = a.dropped		set @UpdatedRecords = @@ROWCOUNTEND[/code]</description><pubDate>Wed, 26 Sep 2012 03:31:44 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Abu Dina (9/26/2012)[/b][hr]Your solution works great but unfortunately the rCTE isn't scaling well enough. With 150000 rows it takes 7 seconds to process. With 160000 records it takes 17 seconds. But when I try 180000+ the query just keeps running and running. Very strange.It might be easier just to stick a loop as Luis suggested as it takes less than 10 seconds to update 350000 records.It's been a good learning exercise and now I have a better understanding of recursive CTEs so not all is lost.Thanks for your efforts Lynn(e) ;-)[/quote]It will scale a great deal better with indexes on the appropriate columns - often I've found clustering on one column and an ordinary index on the other does the trick.</description><pubDate>Wed, 26 Sep 2012 03:06:45 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Your solution works great but unfortunately the rCTE isn't scaling well enough. With 150000 rows it takes 7 seconds to process. With 160000 records it takes 17 seconds. But when I try 180000+ the query just keeps running and running. Very strange.It might be easier just to stick a loop as Luis suggested as it takes less than 10 seconds to update 350000 records.It's been a good learning exercise and now I have a better understanding of recursive CTEs so not all is lost.Thanks for your efforts Lynn(e) ;-)</description><pubDate>Wed, 26 Sep 2012 02:24:32 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Lynn Pettis (9/25/2012)[/b][hr]Here is my code, reformatted the way I like to it.[code="sql"]with rCTE as (select    t1.retained,    t1.dropped,    t1.retained as TopLevelfrom    dbo.testing t1    left outer join dbo.testing t2        on t1.retained = t2.droppedwhere    t2.dropped is nullunion allselect    t1.retained,    t1.dropped,    r.TopLevelfrom    dbo.testing t1    inner join rCTE r        on t1.retained = r.dropped)update tu set    retained = r.TopLevelfrom    dbo.testing tu    inner join rCTE r        on tu.retained = r.retained and           tu.dropped = r.dropped;[/code][/quote]Nice one, Lynn. I was working on something very similar which includes "evidence" in the data that it works;[code="sql"];WITH rCTE AS (	SELECT [level] = 1, t1.retained, t1.dropped,		--fullchain = CAST(CAST(t1.retained AS VARCHAR(10)) + ',' + CAST(t1.dropped AS VARCHAR(10)) AS VARCHAR(8000)),		Toplevel = t1.retained	FROM dbo.testing t1 	LEFT OUTER JOIN dbo.testing t2 ON t1.retained = t2.dropped	WHERE t2.dropped IS NULL	UNION ALL	SELECT [level] = [level] + 1,  t1.retained, t1.dropped, 		--fullchain = r.fullchain + ',' + CAST(t1.dropped AS VARCHAR(10)),		Toplevel = r.Toplevel	FROM dbo.testing t1 	INNER JOIN rCTE r ON t1.retained = r.dropped)--SELECT * --FROM rCTE--ORDER BY level, droppedUPDATE tu SET    retained = r.TopLevelFROM dbo.testing tu INNER JOIN rCTE r ON tu.dropped = r.dropped;SELECT * FROM dbo.testing[/code]Where evidence is the column [fullchain].</description><pubDate>Wed, 26 Sep 2012 02:21:11 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Abu Dina (9/25/2012)[/b][hr][quote][b]Luis Cazares (9/25/2012)[/b][hr]I know it could be a bad solution, and maybe you had tought about it.But since the rCTE is not scaling well (and it sure won't) why don't you use your original solution in a while loop for @@rowcount &amp;gt; 0?I'm not sure which will scale better, but it's still an option.[/quote]Thanks Luis, your suggestion is valid. I see that Lynne has come back with a potential solution. I will test tomorrow and report back.Regards.[/quote]By the way, no 'e' at the end of Lynn.</description><pubDate>Wed, 26 Sep 2012 00:07:04 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Here is my code, reformatted the way I like to it.[code="sql"]with rCTE as (select    t1.retained,    t1.dropped,    t1.retained as TopLevelfrom    dbo.testing t1    left outer join dbo.testing t2        on t1.retained = t2.droppedwhere    t2.dropped is nullunion allselect    t1.retained,    t1.dropped,    r.TopLevelfrom    dbo.testing t1    inner join rCTE r        on t1.retained = r.dropped)update tu set    retained = r.TopLevelfrom    dbo.testing tu    inner join rCTE r        on tu.retained = r.retained and           tu.dropped = r.dropped;[/code]</description><pubDate>Tue, 25 Sep 2012 23:54:46 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Luis Cazares (9/25/2012)[/b][hr]I know it could be a bad solution, and maybe you had tought about it.But since the rCTE is not scaling well (and it sure won't) why don't you use your original solution in a while loop for @@rowcount &amp;gt; 0?I'm not sure which will scale better, but it's still an option.[/quote]Thanks Luis, your suggestion is valid. I see that Lynne has come back with a potential solution. I will test tomorrow and report back.Regards.</description><pubDate>Tue, 25 Sep 2012 14:25:06 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Lynn Pettis (9/25/2012)[/b][hr][quote][b]Abu Dina (9/25/2012)[/b][hr]Lynne's solution works if we assume that the retained ID is always the maximum.But tthe solution doesn't work with the following record set:[code="sql"]drop table dbo.testing create table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 6699, 	157441 union allselect 157441, 73635 union allselect 767884, 	73635 union allselect 1046261, 	73635 union allselect 6699, 	73635 union allselect 1046261, 767884 union allselect 6699, 	767884 union allselect 6699, 1046261[/code]I will keep trying to see if I can come up with a solution but if anyone else can think of something then that'd be great!Thanks in advance.[/quote]I can only write code based on what you provided.  Based on the sample data and expected results, what I saw was the max id being retained.  With the new data, what are the rules for determining what ID is used?  Also, you posted additional data but not additional expected results.[/quote]Okay, let me try this sgain from my phone while eating lunch at McDonalds.Try this, I wrote it on a napkin while eating and looking at the data you posted.[code]with rCTE as (select t1.retained, t1.dropped, t1.retained as TopLevelfrom dbo.testing t1 left outer join dbo.testing t2 on t1.retained = t2.droppedwhere t2.dropped is nullunion allselect t1.retained, t1.dropped, r.TopLevelfrom dbo.testing t1inner join rCTE r on t1.retained = r.dropped)update tu set    retained = r.TopLevelfrom dbo.testing tu inner join rCTE r on tu.retained = r.retained and tu.dropped = r.dropped;[/code]Let me know if it works.  I can't test it on my phone.</description><pubDate>Tue, 25 Sep 2012 13:04:51 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>I know it could be a bad solution, and maybe you had tought about it.But since the rCTE is not scaling well (and it sure won't) why don't you use your original solution in a while loop for @@rowcount &amp;gt; 0?I'm not sure which will scale better, but it's still an option.</description><pubDate>Tue, 25 Sep 2012 11:26:42 GMT</pubDate><dc:creator>Luis Cazares</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Hi Chris,Here is my attempt doing this with a rCTE[code="sql"];WITH retained AS (SELECT t.retained, dropped	FROM testing t	WHERE NOT EXISTS (SELECT 1 FROM testing i WHERE i.dropped = t.retained)	UNION ALL	SELECT b.retained, t1.dropped	FROM testing AS t1	INNER JOIN retained as b		ON t1.retained = b.dropped)				UPDATE t		SET t.retained = uv.retained		FROM testing t		INNER JOIN retained uv			ON t.dropped = uv.dropped[/code]This works with the sample data but something isn't quite right with this. Tried it with a sample of 160000 records, it took 17 seconds.When I increased the sample record set to 200000, the query just keeps going. It's scaling up badly or there's some dodgy data that the above logic just can't handle efficiently. This is what the estimated execution plan is telling me:[img]http://s7.postimage.org/yefdmchcb/executionplan.jpg[/img]Tried adding a couple of indexes on retained and dropped columns but that made no difference to the estimated execution plan.Arrrggghhh! :w00t:</description><pubDate>Tue, 25 Sep 2012 09:43:17 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Abu Dina (9/25/2012)[/b][hr]Thanks Chris,I'm looking at recursive CTEs but I don't think it will work. Well I've been trying for the last couple of hours and it's still not giving the same result as my original update (which I run multiple times until 0 rows are updated!).Thanks for your efforts.[/quote]You're welcome.I tried a rCTE too, and although it does resolve the hierarchy, I couldn't get it to come out in the required format - it would take quite a bit of post-processing which would have made it unfeasibly slow.</description><pubDate>Tue, 25 Sep 2012 07:46:30 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Thanks Chris,I'm looking at recursive CTEs but I don't think it will work. Well I've been trying for the last couple of hours and it's still not giving the same result as my original update (which I run multiple times until 0 rows are updated!).Thanks for your efforts.</description><pubDate>Tue, 25 Sep 2012 07:18:27 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Abu Dina (9/25/2012)[/b][hr]Lynne's solution works if we assume that the retained ID is always the maximum.But tthe solution doesn't work with the following record set:[code="sql"]drop table dbo.testing create table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 6699, 	157441 union allselect 157441, 73635 union allselect 767884, 	73635 union allselect 1046261, 	73635 union allselect 6699, 	73635 union allselect 1046261, 767884 union allselect 6699, 	767884 union allselect 6699, 1046261[/code]I will keep trying to see if I can come up with a solution but if anyone else can think of something then that'd be great!Thanks in advance.[/quote]Yep...[code="sql"];WITH retained AS (	SELECT t.* 	FROM testing t	WHERE NOT EXISTS (SELECT 1 FROM testing i WHERE i.dropped = t.retained))SELECT [Level] = 1, l1.retained, l1.droppedFROM retained l1UNION ALLSELECT [Level] = 2, l1.retained, l2.retainedFROM retained l1inner JOIN testing l2 ON l2.retained = l1.droppedUNION ALLSELECT [Level] = 3, l1.retained, l3.retainedFROM retained l1inner JOIN testing l2 ON l2.retained = l1.droppedinner JOIN testing l3 ON l3.retained = l2.droppedUNION ALLSELECT [Level] = 4, l1.retained, l3.droppedFROM retained l1inner JOIN testing l2 ON l2.retained = l1.droppedinner JOIN testing l3 ON l3.retained = l2.dropped[/code]</description><pubDate>Tue, 25 Sep 2012 06:48:43 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>True... Based on the original sample data it does look like it would work based on maximum id.See my previous reply to ChrisM.As I said, I have a working solution but I'm having to run my update several times until it works. Just wondering if there is an alternative solution which works with one pass.Thanks for your efforts.</description><pubDate>Tue, 25 Sep 2012 06:34:20 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Abu Dina (9/25/2012)[/b][hr]Lynne's solution works if we assume that the retained ID is always the maximum.But tthe solution doesn't work with the following record set:[code="sql"]drop table dbo.testing create table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 6699, 	157441 union allselect 157441, 73635 union allselect 767884, 	73635 union allselect 1046261, 	73635 union allselect 6699, 	73635 union allselect 1046261, 767884 union allselect 6699, 	767884 union allselect 6699, 1046261[/code]I will keep trying to see if I can come up with a solution but if anyone else can think of something then that'd be great!Thanks in advance.[/quote]I can only write code based on what you provided.  Based on the sample data and expected results, what I saw was the max id being retained.  With the new data, what are the rules for determining what ID is used?  Also, you posted additional data but not additional expected results.</description><pubDate>Tue, 25 Sep 2012 06:25:15 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]ChrisM@Work (9/25/2012)[/b][hr]Hi Abu, thank you for the kind mention - I take it that phase of the project is now concluded.[/quote]Hi Chris, not quite! It's a looong story lol .... will explain another time :-P[quote]Have a try with this. It works by first selecting retained rows which don't get a mention in discarded rows, then left joining to self. Hope that makes sense :-)[code="sql"]SELECT a.* FROM testing aLEFT JOIN testing b ON b.retained = a.droppedWHERE NOT EXISTS (SELECT 1 FROM testing i WHERE a.retained = i.dropped)[/code][/quote]Not sure I get you.Here is another sample record set:[code="sql"]drop table dbo.testingcreate table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 972580	, 697688 union allselect 1354938, 	697688 union allselect 1354938	, 972580 union allselect 1555243, 	1354938[/code]The result should be:[img]http://i49.tinypic.com/15pmyv5.jpg[/img]Any ideas?</description><pubDate>Tue, 25 Sep 2012 04:59:23 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Hi Abu, thank you for the kind mention - I take it that phase of the project is now concluded.Have a try with this. It works by first selecting retained rows which don't get a mention in discarded rows, then left joining to self. Hope that makes sense :-)[code="sql"]SELECT a.* FROM testing aLEFT JOIN testing b ON b.retained = a.droppedWHERE NOT EXISTS (SELECT 1 FROM testing i WHERE a.retained = i.dropped)[/code]</description><pubDate>Tue, 25 Sep 2012 02:33:22 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Lynne's solution works if we assume that the retained ID is always the maximum.But tthe solution doesn't work with the following record set:[code="sql"]drop table dbo.testing create table dbo.testing (retained int, dropped int)insert into dbo.testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 6699, 	157441 union allselect 157441, 73635 union allselect 767884, 	73635 union allselect 1046261, 	73635 union allselect 6699, 	73635 union allselect 1046261, 767884 union allselect 6699, 	767884 union allselect 6699, 1046261[/code]I will keep trying to see if I can come up with a solution but if anyone else can think of something then that'd be great!Thanks in advance.</description><pubDate>Tue, 25 Sep 2012 02:24:28 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Nicely done Lynn,I started doing the CTE but you beat me to it..... Much appreciated!</description><pubDate>Mon, 24 Sep 2012 10:32:03 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Phil Parkin (9/24/2012)[/b][hr][quote][b]Lynn Pettis (9/24/2012)[/b][hr]I came up this:--snip[/quote]Interesting image that conjures up. I didn't take in the rest of your post. :w00t:[/quote]Not feeling well today?  ;-)</description><pubDate>Mon, 24 Sep 2012 10:31:29 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>[quote][b]Lynn Pettis (9/24/2012)[/b][hr]I came up this:--snip[/quote]Interesting image that conjures up. I didn't take in the rest of your post. :w00t:</description><pubDate>Mon, 24 Sep 2012 10:30:03 GMT</pubDate><dc:creator>Phil Parkin</dc:creator></item><item><title>RE: Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>I came up this:[code="sql"]create table testing (retained int, dropped int)insert into testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 1055257, 	157441 union allselect 157441, 73635 union allselect 767884, 	73635 union allselect 1046261, 	73635 union allselect 1055257, 	73635 union allselect 1046261, 767884 union allselect 1055257, 	767884 union allselect 1055257, 1046261select * from testingORDER BY dropped,retained;WITH UpdateValues AS (SELECT    MAX(retained) AS NewValue,    droppedFROM    testingGROUP BY    dropped)UPDATE t SET    retained = uv.NewValueFROM    testing t    INNER JOIN UpdateValues uv        ON (t.dropped = uv.dropped);select * from testingORDER BY dropped,retained;DROP TABLE testing;[/code]</description><pubDate>Mon, 24 Sep 2012 10:26:45 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>Consolidating records - TSQL problem</title><link>http://www.sqlservercentral.com/Forums/Topic1363589-392-1.aspx</link><description>Good afternoon,I've been working on a project to eliminate duplicates from a record set containing contact and address details. (ChrisM@Work/Home - thank you so much for all your help!!!!!).I have two columns in my final result set, one containing the ID of the record that I'm keeping and another containing the ID of thr row I am dropping.To concolidate my final result set I've come up with the following solution but I know it's not great because I'm having to run it several times to complete the concolidation:Let me explain with some sample code:[code="sql"]create table testing (retained int, dropped int)insert into testing (retained, dropped)select 767884, 157441 union allselect 1046261, 157441 union allselect 1055257, 	157441 union allselect 157441, 73635 union allselect 767884, 	73635 union allselect 1046261, 	73635 union allselect 1055257, 	73635 union allselect 1046261, 767884 union allselect 1055257, 	767884 union allselect 1055257, 1046261select * from testing-- consoidate records:-- updates 6 records:update bset b.retained=a.retainedfrom testing as ainner join testing as bon b.retained = a.dropped-- updates remaining 3update bset b.retained=a.retainedfrom testing as ainner join testing as bon b.retained = a.droppedselect * from testingdrop table testing[/code]See below image, left table is what I started with, right table is the result I want:[img]http://s12.postimage.org/ym6i1ktrx/Consolidate_Records.jpg[/img]My solution works but I'm sure there is a better way to do this. Any suggestions to do this in one pass?!Thanks in advance.</description><pubDate>Mon, 24 Sep 2012 09:59:14 GMT</pubDate><dc:creator>Abu Dina</dc:creator></item></channel></rss>