{"id":542,"date":"2011-08-01T13:47:00","date_gmt":"2011-08-01T13:47:00","guid":{"rendered":"\/blogs\/bobb\/post\/An-FAQ-about-the-window-clause-in-SQL-Server-Denali.aspx"},"modified":"2011-08-01T13:47:00","modified_gmt":"2011-08-01T13:47:00","slug":"an-faq-about-the-window-clause-in-sql-server-denali","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/","title":{"rendered":"An FAQ about the window clause in SQL Server Denali"},"content":{"rendered":"<p>\nI&#39;ve been working with the new Denali T-SQL windowing functionality and ran into someone who asked about this &quot;problem&quot;. It&#39;s almost sure to become an FAQ.\n<\/p>\n<p>\nWhy does last_value not always &quot;work right&quot;?\n<\/p>\n<p>\nTake, as an example, the following query (against Adventureworks2008R2)\n<\/p>\n<p>\nselect&nbsp; AccountNumber,&nbsp; SalesPersonID, SalesOrderID,<br \/>\n&nbsp;first_value(SalesOrderID) over(partition by AccountNumber order by SalesOrderID) as FirstOrderForAcct, <br \/>\n&nbsp;last_value(SalesOrderID) over(partition by AccountNumber&nbsp; order by SalesOrderID) as LastOrderForAcct<br \/>\nfrom Sales.SalesOrderHeader<br \/>\nwhere SalesPersonID IS NOT NULL\n<\/p>\n<p>\nWhen you look as the results, FirstOrderForAcct seems OK, but LastOrderForAcct always returns the same number as the current account for each row. Not the last number. Why?\n<\/p>\n<p>\nThe reason is that, when using first\/last_value with a window of rows, the default window is &quot;range between unbounded preceding and current row&quot;. So the query above with the &quot;weird&quot; result is equivalent to this one.\n<\/p>\n<p>\nselect&nbsp; AccountNumber,&nbsp; SalesPersonID, SalesOrderID,<br \/>\n&nbsp;first_value(SalesOrderID) over(partition by AccountNumber order by SalesOrderID<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>range between unbounded preceding and current row<\/strong>) as FirstOrderForAcct, <br \/>\n&nbsp;last_value(SalesOrderID) over(partition by AccountNumber&nbsp; order by SalesOrderID<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>range between unbounded preceding and current row<\/strong>) as LastOrderForAcct<br \/>\nfrom Sales.SalesOrderHeader<br \/>\nwhere SalesPersonID IS NOT NULL\n<\/p>\n<p>\nIf you want the &quot;intuitive&quot; answer for last_value, just change the window clause to:\n<\/p>\n<p>\n<br \/>\nselect&nbsp; AccountNumber,&nbsp; SalesPersonID, SalesOrderID,<br \/>\n&nbsp;first_value(SalesOrderID) over(partition by AccountNumber order by SalesOrderID<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>range between unbounded preceding and unbounded following<\/strong>) as FirstOrderForAcct, <br \/>\n&nbsp;last_value(SalesOrderID) over(partition by AccountNumber&nbsp; order by SalesOrderID<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>range between unbounded preceding and unbounded following<\/strong>) as LastOrderForAcct<br \/>\nfrom Sales.SalesOrderHeader<br \/>\nwhere SalesPersonID IS NOT NULL\n<\/p>\n<p>\n@bobbeauch<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve been working with the new Denali T-SQL windowing functionality and ran into someone who asked about this &quot;problem&quot;. It&#39;s almost sure to become an FAQ. Why does last_value not always &quot;work right&quot;? Take, as an example, the following query (against Adventureworks2008R2) select&nbsp; AccountNumber,&nbsp; SalesPersonID, SalesOrderID, &nbsp;first_value(SalesOrderID) over(partition by AccountNumber order by SalesOrderID) as FirstOrderForAcct, [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,40],"tags":[],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-sql-server-2012","category-transact-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>An FAQ about the window clause in SQL Server Denali - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An FAQ about the window clause in SQL Server Denali - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"I&#039;ve been working with the new Denali T-SQL windowing functionality and ran into someone who asked about this &quot;problem&quot;. It&#039;s almost sure to become an FAQ. Why does last_value not always &quot;work right&quot;? Take, as an example, the following query (against Adventureworks2008R2) select&nbsp; AccountNumber,&nbsp; SalesPersonID, SalesOrderID, &nbsp;first_value(SalesOrderID) over(partition by AccountNumber order by SalesOrderID) as FirstOrderForAcct, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2011-08-01T13:47:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/\",\"name\":\"An FAQ about the window clause in SQL Server Denali - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2011-08-01T13:47:00+00:00\",\"dateModified\":\"2011-08-01T13:47:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server 2012\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2012\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"An FAQ about the window clause in SQL Server Denali\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An FAQ about the window clause in SQL Server Denali - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/","og_locale":"en_US","og_type":"article","og_title":"An FAQ about the window clause in SQL Server Denali - Bob Beauchemin","og_description":"I&#39;ve been working with the new Denali T-SQL windowing functionality and ran into someone who asked about this &quot;problem&quot;. It&#39;s almost sure to become an FAQ. Why does last_value not always &quot;work right&quot;? Take, as an example, the following query (against Adventureworks2008R2) select&nbsp; AccountNumber,&nbsp; SalesPersonID, SalesOrderID, &nbsp;first_value(SalesOrderID) over(partition by AccountNumber order by SalesOrderID) as FirstOrderForAcct, [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/","og_site_name":"Bob Beauchemin","article_published_time":"2011-08-01T13:47:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/","name":"An FAQ about the window clause in SQL Server Denali - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2011-08-01T13:47:00+00:00","dateModified":"2011-08-01T13:47:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/an-faq-about-the-window-clause-in-sql-server-denali\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"SQL Server 2012","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2012\/"},{"@type":"ListItem","position":3,"name":"An FAQ about the window clause in SQL Server Denali"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}