{"id":187,"date":"2007-08-13T13:13:00","date_gmt":"2007-08-13T13:13:00","guid":{"rendered":"\/lisa\/post\/2007\/08\/13\/Partition-Magic.aspx"},"modified":"2007-08-13T13:13:00","modified_gmt":"2007-08-13T13:13:00","slug":"partition-magic","status":"publish","type":"post","link":"https:\/\/spacefold.com\/lisa\/2007\/08\/13\/partition-magic\/","title":{"rendered":"Partition Magic"},"content":{"rendered":"<p>\nSometimes a T-SQL gal just has to lower her standards. ANSI standards, that is.\n<\/p>\n<p>\nYou&#39;re sitting there with a tough nut to crack and MS SQL Server just has such a &#8230; sweet&#8230; resolution for it that&#8230;\n<\/p>\n<p>\nYou just gotta admit that you&#39;re probably not ever going to migrate this database in this lifetime and that, if you did, Oracle probably has an equivalently sweet solution, spelled one or two letters differently, and you&#39;ll Cross That Bridge When You Come To It.\n<\/p>\n<h5>Case in point<\/h5>\n<p>\nI have a project where we&#39;re pulling a list of &quot;candidates&quot; for a customer service survey.&nbsp; The rules are really complicated for selection, but the pseudocode for the query look like this:\n<\/p>\n<p>\nSELECT Account, Email, ContactName FROM<br \/>\n&nbsp;&nbsp; [ReallyComplicatedContactView] <br \/>\nWHERE<br \/>\n&nbsp;&nbsp; [Millions of contact-filtering conditions here] AND<br \/>\n&nbsp;&nbsp; [Lots more time-sensitive conditions here]<br \/>\nORDER BY [priorities du jour here]\n<\/p>\n<p>\nSo now we decide that we also don&#39;t want to select more than &lt;target number&gt; candidates from any one Account, at any one time, for this survey. Bleh.\n<\/p>\n<p>\n<a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms189461\/\" title=\"Ranking functions in T-SQL\">T-SQL 2005 to the rescue<\/a>&#8230;\n<\/p>\n<p>\nSELECT Account, Email, ContactName, AcctRank FROM <br \/>\n&nbsp;&nbsp; (SELECT V.Account, V.Email, V.ContactName ,&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: red\">ROW_NUMBER() <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; OVER (PARTITION BY Account <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY Account ASC, [Priorities du jour] ASC ) <br \/>\n&nbsp;&nbsp; AS AcctRank <\/span><br \/>\n&nbsp;&nbsp; FROM <br \/>\n&nbsp;&nbsp; [ReallyComplicatedContactView] V <br \/>\n&nbsp;&nbsp; WHERE <br \/>\n&nbsp;&nbsp; [as before] AND<br \/>\n&nbsp;&nbsp; [as before]<br \/>\n&nbsp;&nbsp; ) XX<br \/>\nWHERE <span style=\"color: red\">XX.AcctRank &lt;= @TargetNumber<\/span><br \/>\nORDER BY [as before]\n<\/p>\n<p>\nThat&#39;s almost too easy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes a T-SQL gal just has to lower her standards. ANSI standards, that is. You&#39;re sitting there with a tough nut to crack and MS SQL Server just has such a &#8230; sweet&#8230; resolution for it that&#8230; You just gotta admit that you&#39;re probably not ever going to migrate this database in this lifetime and<a class=\"more-link\" href=\"https:\/\/spacefold.com\/lisa\/2007\/08\/13\/partition-magic\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}