Use Window Functions to perform calculations<\/h2>\n\n\n\nTo make this useful for the Sales Manager we can use CAST and a new column to calculate the difference between the dates.<\/p>\n\n\n\n
\n\n\n\nselect\n customerid, \n subtotal, \n sale_date,\n lag(sale_date) over(order by sale_date) as last_order,\n cast(sale_date - (lag(sale_date) \nover(order by sale_date)) as int) as days_between\nfrom sales.salesorderheader \nwhere \n customerid = 11078\n and sale_date between '2018-01-01' and '2019-01-01'<\/code><\/pre>\n\n\n\n \n\n\n\n<\/figure>\n\n\n\n \n\n\n\nI hope you\u2019ve enjoyed these two posts on SQL Window Functions. How do you use these in your work? Do you find them a useful and powerful tool?<\/p>\n\n\n\n
\n\n\n\nPhoto by Nubia Navarro (nubikini)<\/a><\/strong> from Pexels<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"
SQL Server Window Functions are incredibly powerful and allow us to access a total from another row and bucket data to show top performers.<\/p>\n","protected":false},"author":1,"featured_media":3066,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":""},"categories":[214],"tags":[198,199],"yoast_head":"\nWhy you need SQL Window Functions (part 2) | Helen Anderson<\/title>\n<meta name=\"description\" content=\"SQL Server Window Functions are incredibly powerful and allow us to access a total from another row and bucket data to show top performers.\" \/>\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.helenanderson.co.nz\/sql-window-functions-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Why you need SQL Window Functions (part 2) | Helen Anderson\" \/>\n<meta property=\"og:description\" content=\"SQL Server Window Functions are incredibly powerful and allow us to access a total from another row and bucket data to show top performers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Helen Anderson\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-21T07:52:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-25T08:18:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/helenanderson.co.nz\/wp-content\/uploads\/2020\/05\/sql-window-functions-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"853\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@helenanders26\" \/>\n<meta name=\"twitter:site\" content=\"@helenanders26\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"3 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/13.237.200.153\/#website\",\"url\":\"https:\/\/13.237.200.153\/\",\"name\":\"Helen Anderson\",\"description\":\"Data Analyst | Technical Writer\",\"publisher\":{\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/13.237.200.153\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#primaryimage\",\"inLanguage\":\"en-NZ\",\"url\":\"https:\/\/helenanderson.co.nz\/wp-content\/uploads\/2020\/05\/sql-window-functions-2.jpg\",\"width\":1280,\"height\":853,\"caption\":\"sql window functions\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#webpage\",\"url\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/\",\"name\":\"Why you need SQL Window Functions (part 2) | Helen Anderson\",\"isPartOf\":{\"@id\":\"https:\/\/13.237.200.153\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#primaryimage\"},\"datePublished\":\"2019-07-21T07:52:18+00:00\",\"dateModified\":\"2020-05-25T08:18:41+00:00\",\"description\":\"SQL Server Window Functions are incredibly powerful and allow us to access a total from another row and bucket data to show top performers.\",\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/\"]}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#webpage\"},\"author\":{\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"headline\":\"Why you need SQL Window Functions (part 2)\",\"datePublished\":\"2019-07-21T07:52:18+00:00\",\"dateModified\":\"2020-05-25T08:18:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"image\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-window-functions-part-2\/#primaryimage\"},\"keywords\":\"analysis,sql\",\"articleSection\":\"Data Analysis\",\"inLanguage\":\"en-NZ\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\",\"name\":\"Helen Anderson\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/13.237.200.153\/#personlogo\",\"inLanguage\":\"en-NZ\",\"url\":\"https:\/\/helenanderson.co.nz\/wp-content\/uploads\/2019\/11\/helen-anderson-profile-selects-FA-1000.jpg\",\"width\":1000,\"height\":1000,\"caption\":\"Helen Anderson\"},\"logo\":{\"@id\":\"https:\/\/13.237.200.153\/#personlogo\"},\"description\":\"Hi, I'm Helen. I'm a data analyst, technical writer, and AWS Data Hero. I interpret the story behind the numbers, build data applications, and grow analyst and developer communities - currently at Kiwibank.\",\"sameAs\":[\"http:\/\/www.helenanderson.co.nz\/\",\"https:\/\/www.instagram.com\/helenanders26\/\",\"https:\/\/www.linkedin.com\/in\/helenanders26\/\",\"https:\/\/twitter.com\/helenanders26\",\"https:\/\/www.youtube.com\/channel\/UCttVhJizwkhgmMlDBMUE0wQ\"]}]}<\/script>\n","_links":{"self":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/3065"}],"collection":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/comments?post=3065"}],"version-history":[{"count":6,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/3065\/revisions"}],"predecessor-version":[{"id":3298,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/3065\/revisions\/3298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/media\/3066"}],"wp:attachment":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/media?parent=3065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/categories?post=3065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/tags?post=3065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}