<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

      <title>Comments on: How do to update rows without a cursor?</title>
      <link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor/</link>
      <description>Comments on Ask MetaFilter post How do to update rows without a cursor?</description>
	  	  <pubDate>Wed, 24 Jan 2007 13:21:44 -0800</pubDate>
      <lastBuildDate>Wed, 24 Jan 2007 13:21:44 -0800</lastBuildDate>
      <language>en-us</language>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <ttl>60</ttl>

<item>
  	<title>Question: How do to update rows without a cursor?</title>
  	<link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor</link>	
  	<description>How do I update rows in a table in a SQL*Server 2005 database with data from another row in the same table? &lt;br /&gt;&lt;br /&gt; I have an event log type table that contains data as follows:&lt;br&gt;
EVENT_TYPE     START_TIME     END_TIME&lt;br&gt;
type1                10&lt;br&gt;
type3                4&lt;br&gt;
type2                5&lt;br&gt;
type1                11&lt;br&gt;
type1                15&lt;br&gt;
type2                7&lt;br&gt;
type2                9&lt;br&gt;
type3                15&lt;br&gt;
type1                23&lt;br&gt;
&lt;br&gt;
For whatever reason, it turns out that I need to create a column called END_TIME and populate that with the BEGIN_TIME of the next event with the same EVENT_TYPE.&lt;br&gt;
&lt;br&gt;
So, I would need to end up with&lt;br&gt;
&lt;br&gt;
EVENT_TYPE     START_TIME     END_TIME&lt;br&gt;
type1                10                    11&lt;br&gt;
type3                4                      15&lt;br&gt;
type2                5                      7&lt;br&gt;
type1                11                    15&lt;br&gt;
type1                15                    23&lt;br&gt;
type2                7                      9&lt;br&gt;
type2                9                      x&lt;br&gt;
type3                15                    x&lt;br&gt;
type1                23                    x&lt;br&gt;
&lt;br&gt;
So, if you see the first &quot;type1&quot; event, the END_TIME is the START_TIME of the next &quot;type1&quot; event which is 11.&lt;br&gt;
&lt;br&gt;
Now, I suppose I can use a cursor to do this, but this DB has 10s of millions of rows and it&apos;s likely to take days, if not weeks to do this using a cursor. I&apos;ve been Googling around and I see that some kind of Set base approach performs much better than a Cursor based approach.&lt;br&gt;
&lt;br&gt;
My SQL-fu fails me here. Can you help? &lt;br&gt;
&lt;br&gt;
FYI, I&apos;m using SQL*Server 2005. I&apos;m pertty weak at T-SQL so,... TIA!</description>
  	<guid isPermaLink="false">post:ask.metafilter.com,2008:site.55669</guid>
  	<pubDate>Wed, 24 Jan 2007 13:02:04 -0800</pubDate>
  	<dc:creator>apark</dc:creator>
	
	<category>database</category>
	
	<category>SQL</category>
	
	<category>query</category>
	
	<category>update</category>
	
	<category>rows</category>
	
</item>
<item>
  	<title>By: Doofus Magoo</title>
  	<link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor#837855</link>	
  	<description>A subquery should do the trick, but I&apos;m not sure what the performance would be like. I tested this using your sample dataset, and it seemed to work fine:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;UPDATE [tablename]&lt;br&gt;
SET END_TIME=&lt;br&gt;
(&lt;br&gt;
SELECT MIN(START_TIME) FROM [tablename] b&lt;br&gt;
WHERE event_type=b.event_type&lt;br&gt;
AND b.START_TIME&amp;gt;tablename.START_TIME&lt;br&gt;
)&lt;/pre&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.55669-837855</guid>
  	<pubDate>Wed, 24 Jan 2007 13:21:44 -0800</pubDate>
  	<dc:creator>Doofus Magoo</dc:creator>
</item>
<item>
  	<title>By: Doofus Magoo</title>
  	<link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor#837861</link>	
  	<description>Whoops, scratch that. It&apos;s updating them absolutely, not based on EVENT_TYPE. It threw me when I was looking at the results because all of the type1 come before all of the type2, which come before all of the type3. Sorry about the confusion.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.55669-837861</guid>
  	<pubDate>Wed, 24 Jan 2007 13:26:23 -0800</pubDate>
  	<dc:creator>Doofus Magoo</dc:creator>
</item>
<item>
  	<title>By: Doofus Magoo</title>
  	<link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor#837864</link>	
  	<description>This should do the trick. It&apos;s the same as my first post, but I explicitly reference the table in the outer part of the nested query:&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;UPDATE [tablename]&lt;br&gt;
SET END_TIME=&lt;br&gt;
(&lt;br&gt;
SELECT MIN(START_TIME) FROM [tablename] b&lt;br&gt;
WHERE &lt;u&gt;tablename&lt;/u&gt;.event_type=b.event_type&lt;br&gt;
AND b.START_TIME&amp;gt;tablename.START_TIME&lt;br&gt;
)&lt;/pre&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.55669-837864</guid>
  	<pubDate>Wed, 24 Jan 2007 13:27:45 -0800</pubDate>
  	<dc:creator>Doofus Magoo</dc:creator>
</item>
<item>
  	<title>By: apark</title>
  	<link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor#837899</link>	
  	<description>Thanks Doofus Magoo. Looks plausible to me. I just kicked it off on the server. Let&apos;s see what happens!</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.55669-837899</guid>
  	<pubDate>Wed, 24 Jan 2007 13:53:52 -0800</pubDate>
  	<dc:creator>apark</dc:creator>
</item>
<item>
  	<title>By: apark</title>
  	<link>http://ask.metafilter.com/55669/How-do-to-update-rows-without-a-cursor#837972</link>	
  	<description>Doofus Magoo, you&apos;re the man. It worked like a charm. Thank you!</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.55669-837972</guid>
  	<pubDate>Wed, 24 Jan 2007 15:01:39 -0800</pubDate>
  	<dc:creator>apark</dc:creator>
</item>

    </channel>
</rss>
