<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Rob Farley : cte</title><link>http://msmvps.com/blogs/robfarley/archive/tags/cte/default.aspx</link><description>Tags: cte</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Set-based approach to finding consecutive records</title><link>http://msmvps.com/blogs/robfarley/archive/2007/01/17/set-based-approach-to-finding-consecutive-records.aspx</link><pubDate>Wed, 17 Jan 2007 00:15:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:498822</guid><dc:creator>Rob Farley</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/robfarley/rsscomments.aspx?PostID=498822</wfw:commentRss><comments>http://msmvps.com/blogs/robfarley/archive/2007/01/17/set-based-approach-to-finding-consecutive-records.aspx#comments</comments><description>&lt;p&gt;I'm sure I've written about this before, but it comes up so regularly in the newsgroups that I thought I'd write an answer here.&lt;/p&gt;&lt;p&gt;A table (probably an audit table) exists with a field that stores a date signifying when a change was made. You need to return a result-set which has a row for each period of time between modifications.&lt;/p&gt;&lt;p&gt;For example:&lt;/p&gt;&lt;p&gt;&lt;i&gt;CREATE TABLE QtyAdj ( .... , ModDate DATETIME, Qty INT )&lt;/i&gt;&lt;/p&gt;&lt;p&gt;--where I haven't listed all the columns - there should obviously be a primary key on this. In fact, I'll assume there is a field called ID which is unique.&lt;br&gt;&lt;/p&gt;&lt;p&gt;What we want is:&lt;/p&gt;&lt;p&gt;&lt;i&gt;with modnums as (select *, row_number() over (order by moddate, id) as rn from 
qtyadj)&lt;br&gt;select m_this.moddate, m_next.moddate, m_this.qty&lt;br&gt;from&lt;br&gt;&amp;nbsp; 
modnums m_this&lt;br&gt;&amp;nbsp; left join&lt;br&gt;&amp;nbsp; modnums m_next&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; on m_next.rn = 
m_this.rn + 1&lt;/i&gt;&lt;/p&gt;&lt;p&gt;Which uses row_number() within a table expression to give a unique number to each row ordered by the date. You can then join between two copies of the table expression (love CTEs for that), to get your consecutive records in a single row of your result-set.&lt;br&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=498822" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/robfarley/archive/tags/cte/default.aspx">cte</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/sql/default.aspx">sql</category></item><item><title>Faster binary converter - where do your solutions come from?</title><link>http://msmvps.com/blogs/robfarley/archive/2006/10/24/Faster-binary-converter-_2D00_-where-do-your-solutions-come-from_3F00_.aspx</link><pubDate>Mon, 23 Oct 2006 23:53:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:203643</guid><dc:creator>Rob Farley</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/robfarley/rsscomments.aspx?PostID=203643</wfw:commentRss><comments>http://msmvps.com/blogs/robfarley/archive/2006/10/24/Faster-binary-converter-_2D00_-where-do-your-solutions-come-from_3F00_.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://mitch-wheat.blogspot.com/" target="_blank"&gt;Mitch Wheat&lt;/a&gt; is a great guy. He really is. And he&amp;#39;s clearly a mathematician.&lt;/p&gt;&lt;p&gt;I mean, I did a  half-major as part of my BSc, doing a handful of 3rd year Pure Maths subjects (which I got HDs in too - I wasn&amp;#39;t just sleeping during class), but Mitch... he thinks like a mathematician. You can tell when he comes up with a solution like &lt;a href="http://mitch-wheat.blogspot.com/2006/10/t-sql-way-converting-integers-to.html" target="_blank"&gt;this for converting numbers to binary&lt;/a&gt;. Makes me wonder if this is something he came up with, or something he read in a book. Knowing Mitch, he came up with it himself. I&amp;#39;m in a bit of awe here. I feel like it&amp;#39;s the kind of solution that I could&amp;#39;ve come up with once upon a time, and hopefully could still given the right colleagues and a whiteboard. :)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Let&amp;#39;s come back to the &amp;#39;solution read in a book&amp;#39; thing. &lt;/p&gt;&lt;p&gt;I remember sitting in a user-group meeting in Redmond in March. &lt;a href="http://sql.co.il/" target="_blank"&gt;Itzik&lt;/a&gt; was speaking. He was talking about the use of CLR in SQL, and came across the Product aggregate function. There is no product() in T-SQL, and a lot of people have considered that it&amp;#39;s a good candidate for CLR, since you can make a user-defined aggregate function this way. Itzik&amp;#39;s lines were something like &amp;quot;And I was sure there was a good way of doing this without having to use CLR, so I got reading my maths books.&amp;quot; He mentioned some of the names that I have a vague recollection of from my university days (which I have forgotten again so won&amp;#39;t write here), and then said that he came across &lt;em&gt;log(a*b) = log(a) + log(b)&lt;/em&gt;. And of course, almost everyone in the room had light-bulbs appear. &lt;br /&gt;&lt;/p&gt;&lt;p&gt;I wasn&amp;#39;t so much inspired by the fact that &amp;quot;here&amp;#39;s this great way of solving the product question&amp;quot;, but rather &amp;quot;here&amp;#39;s a way of solving some miscellaneous thing, where the answer comes out of a maths book.&amp;quot; And now I&amp;#39;ve found Mitch providing me with the same hunger to academise (that&amp;#39;s not a word, but you know what I mean).&lt;/p&gt;&lt;p&gt;I nearly got into academia. Having done uni, I had offers to do a PhD. But I had a good job offer, and studying didn&amp;#39;t seem like it was going to pay the bills. A large part of me would&amp;#39;ve liked to have done. I remember being a research assistant for Professor John Crossley at Monash, helping to write code (in CAM/L, using a Tcl/Tk interface) to try to convert mathematical proofs into computer programs (the idea being that if you can prove - using really fundamental principles - that for all x, there exists a number y that has particular properties, then that proof should be able to be converted &amp;#39;automatically&amp;#39; into a program which will find that y). I found that fascinating. I don&amp;#39;t know whether the theory ever got really proven, but I found academia really interesting.&lt;/p&gt;&lt;p&gt;And of course, in the &amp;#39;real&amp;#39; world, the uses of academia are somewhat doubted. It was good to see a locknote session at Tech.Ed (AU) this year about some of the image processing stuff that Microsoft Research Labs are working on. But I&amp;#39;m also really curious to know about algorithms for solving interesting problems.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=203643" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/robfarley/archive/tags/cte/default.aspx">cte</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/sql/default.aspx">sql</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/academia/default.aspx">academia</category></item><item><title>Simple recursive CTE</title><link>http://msmvps.com/blogs/robfarley/archive/2006/10/20/Simple-recursive-CTE.aspx</link><pubDate>Fri, 20 Oct 2006 09:26:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:191447</guid><dc:creator>Rob Farley</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/robfarley/rsscomments.aspx?PostID=191447</wfw:commentRss><comments>http://msmvps.com/blogs/robfarley/archive/2006/10/20/Simple-recursive-CTE.aspx#comments</comments><description>&lt;p&gt;I like &lt;a href="http://msdn2.microsoft.com/en-us/library/ms190766.aspx" target="_blank"&gt;CTEs&lt;/a&gt;. Mostly because it lets me have a derived table that I can refer to multiple times. I don&amp;#39;t often use the recursive features of it. But when I do, I repeatedly feel amazed at the power available.&lt;/p&gt;&lt;p&gt;Today I had a challenge to produce a nice SQL way of &lt;a href="http://omnibuzz-sql.blogspot.com/2006/10/scenario-to-ponder-5.html" target="_blank"&gt;converting integers to binary&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;&lt;p&gt;So I pulled out the CTE. :)&lt;/p&gt;&lt;p&gt;with ctebins as&lt;br /&gt;(select num as num_orig, num as working_level, cast(&amp;#39;&amp;#39; as varchar(max)) as binval&lt;br /&gt;from nums_bin&lt;br /&gt;union all&lt;br /&gt;select c.num_orig, c.working_level / 2, cast(c.working_level % 2 as varchar(max)) + c.binval&lt;br /&gt;from ctebins c&lt;br /&gt;where c.working_level &amp;gt; 0&lt;br /&gt;)&lt;br /&gt;select num_orig, binval&lt;br /&gt;from ctebins&lt;br /&gt;where working_level = 0&lt;br /&gt;;&lt;br /&gt;&lt;br /&gt;What this is doing is to populate the top level of the CTE with a bunch of numbers from nums_bin, along with a blank string. I know I don&amp;#39;t have to use varchar(max) - it&amp;#39;s not going to get that big after all...&lt;/p&gt;&lt;p&gt;Then it fills the CTE with rows using recursion. I keep the original number (to use later), and keep cutting it in half to get the binary number out. Then I query the full CTE for the last level of recursion - when my working number got down to zero.&lt;/p&gt;&lt;p&gt;It works nicely, and it&amp;#39;s very fast on a big set of numbers.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=191447" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/robfarley/archive/tags/cte/default.aspx">cte</category><category domain="http://msmvps.com/blogs/robfarley/archive/tags/sql/default.aspx">sql</category></item></channel></rss>