{"id":6502,"date":"2019-03-12T09:31:53","date_gmt":"2019-03-12T16:31:53","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=6502"},"modified":"2025-06-13T17:22:10","modified_gmt":"2025-06-14T00:22:10","slug":"get-a-bigger-picture-with-n1ql-window-functions-and-cte","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/","title":{"rendered":"Get a Bigger Picture with N1QL Window Functions and CTE"},"content":{"rendered":"<p>Whether it is a personal need to understand your spending pattern from looking at the bank transactions, or seeking to improve the sales performance in your organization by looking at the sales activities, the real insights are only visible with additional aggregation and summarization of the transactional level data. Traditional RDBMS provides this capability through the expressive power of the SQL aggregation. Window functions was added to the ANSI SQL standard to further extend the ability to express more complex questions with the SQL construct.<\/p>\n<p>Couchbase N1QL now supports the window functions and common table expression (CTE) in its 6.5 release. Developers can extend their applications to meet more complex business use cases, as well as empowering data analysts to get the answers they need without performing additional post processing in Excel.<\/p>\n<p>For this article I will focus on couple of examples on how you can leverage N1QL Window functions and CTE to address two very common business questions.<\/p>\n<h3>Sales activities duration by customers<\/h3>\n<p>In this first example for a sales activity management system, we want to provide a report to show the amount of time that the sales team has spent working with their customers for\u00a0 January-2019. I will break the query down into two steps:<\/p>\n<p>1) Get a list of meetings and the meeting duration that the sales team has conducted with their customers. The total time spent for all customers &#8216;total_time_spent&#8217; is calculated by summing the duration of the appointments with an empty OVER () clause, which will perform the summing for the entire result set. The total time spent by customer\u00a0 &#8216;account_time_spent&#8217; uses the same construct, but with the &#8216;accid&#8217; for the PARTITION clause.<\/p>\n<pre class=\"font:courier-new show-lang:2 lang:mysql decode:true\">SELECT  c.name, a.startDate, a.title, a.duration, \r\n        SUM(a.duration) OVER() as total_time_spent,\r\n        SUM(a.duration) OVER(PARTITION BY a.accid) AS account_time_spent\r\nFROM crm a\r\n  INNER JOIN crm c ON a.accid = c.id AND  c.type='account'\r\nWHERE a.type='activity'\r\nAND a.activityType='Appointment'\r\nAND a.startDate BETWEEN '2018-01-01' and '2018-01-31'\r\n<\/pre>\n<pre class=\"font:courier-new tab-convert:true lang:mysql decode:true\">account_time_spent\tduration\tname\tstartDate\ttitle\ttotal_time_spent\r\n30\t\"30\"\t\"Smith, Avila and Cisneros\"\t\"2018-01-24 15:00:00\"\t\"Switchable coherent adapter\"\t25770\r\n510\t\"150\"\t\"Riddle Ltd\"\t\"2018-01-05 15:30:00\"\t\"Streamlined intermediate intranet\"\t25770\r\n510\t\"90\"\t\"Riddle Ltd\"\t\"2018-01-28 14:30:00\"\t\"Advanced solution-oriented synergy\"\t25770\r\n510\t\"90\"\t\"Riddle Ltd\"\t\"2018-01-01 15:00:00\"\t\"Ameliorated object-oriented methodology\"\t25770\r\n510\t\"30\"\t\"Riddle Ltd\"\t\"2018-01-10 08:00:00\"\t\"Object-based multi-state firmware\"\t25770\r\n510\t\"150\"\t\"Riddle Ltd\"\t\"2018-01-23 09:00:00\"\t\"Multi-layered systematic software\"\t25770\r\n120\t\"30\"\t\"Foster Inc\"\t\"2018-01-29 09:30:00\"\t\"Public-key bottom-line database\"\t25770\r\n120\t\"30\"\t\"Foster Inc\"\t\"2018-01-09 13:00:00\"\t\"Quality-focused local emulation\"\t25770\r\n120\t\"60\"\t\"Foster Inc\"\t\"2018-01-02 08:00:00\"\t\"Digitized motivating matrix\"\t25770\r\n120\t\"30\"\t\"Williams Ltd\"\t\"2018-01-22 08:30:00\"\t\"Versatile heuristic workforce\"\t25770\r\n120\t\"30\"\t\"Williams Ltd\"\t\"2018-01-24 13:30:00\"\t\"Front-line 4thgeneration help-desk\"\t25770\r\n120\t\"60\"\t\"Williams Ltd\"\t\"2018-01-14 14:30:00\"\t\"Visionary upward-trending success\"\t25770\r\n330\t\"150\"\t\"Reid Ltd\"\t\"2018-01-21 10:30:00\"\t\"Profound logistical archive\"\t25770\r\n330\t\"30\"\t\"Reid Ltd\"\t\"2018-01-13 13:30:00\"\t\"Down-sized coherent access\"\t25770\r\n330\t\"120\"\t\"Reid Ltd\"\t\"2018-01-02 12:00:00\"\t\"Front-line object-oriented moderator\"\t25770\r\n330\t\"30\"\t\"Reid Ltd\"\t\"2018-01-12 09:30:00\"\t\"Programmable reciprocal infrastructure\"\t25770\r\n....<\/pre>\n<p>2) Then use the two metrics to derive the percentage of the overall time the team spent with each customer.<\/p>\n<pre class=\"font:courier-new lang:mysql decode:true \">SELECT  c.name,  \r\n  ROUND(( SUM(SUM(a.duration)) OVER(PARTITION BY a.accid)  \/ \r\n   SUM(SUM(a.duration)) OVER()),2) as pct_time\r\nFROM crm a\r\nINNER JOIN crm c ON a.accid = c.id AND  c.type='account'\r\nWHERE a.type='activity'\r\n  AND a.activityType='Appointment'\r\n  AND a.startDate BETWEEN '2018-01-01' and '2018-01-31'\r\nGROUP BY c.name, a.accid\r\nORDER BY 2 DESC<\/pre>\n<pre class=\"font:courier-new lang:default decode:true\">name\tpct_time\r\n\"Johnson, Adams and Kelly\"\t0.17\r\n\"Davis Group\"\t0.08\r\n\"Gilbert-Morris\"\t0.08\r\n\"Torres and Sons\"\t0.07\r\n\"Reid Ltd\"\t0.07\r\n\"Medina-Daniels\"\t0.07\r\n\"Riddle Ltd\"\t0.05\r\n\"Henderson and Sons\"\t0.05\r\n\"Gill and Sons\"\t0.05\r\n\"Garcia-Young\"\t0.05\r\n\"Sullivan PLC\"\t0.03\r\n\"Brown-Rogers\"\t0.03\r\n\"Foster Inc\"\t0.03\r\n\"Wheeler Inc\"\t0.03\r\n\"Jarvis-Small\"\t0.03\r\n\"Jones-Fox\"\t0.03\r\n\"Lloyd, Blair and Pruitt\"\t0.03\r\n\"Vaughn LLC\"\t0.02\r\n<\/pre>\n<h3>Sales Activities Month-over-Month<\/h3>\n<p>In this second example, the query shows how the number of sales related tasks have changed month over month for the year 2018.\u00a0 The query leverages N1QL CTE feature to improve readability of the query, an also the LAG window function to obtain the\u00a0 previous period task count.<\/p>\n<pre class=\"font:courier-new lang:mysql decode:true\">WITH current_period_task AS (\r\n  SELECT DATE_TRUNC_STR(a.startDate,'month') AS month,\r\n         COUNT(1) AS current_period_task_count\r\n  FROM crm a \r\n  WHERE a.type='activity' AND a.activityType = 'Task'\r\n    AND   DATE_PART_STR(a.startDate,'year') = 2018\r\n    GROUP BY DATE_TRUNC_STR(a.startDate,'month')\r\n), \r\nlast_period_task AS ( \r\n   SELECT x.month, x.current_period_task_count, \r\n          LAG(x.current_period_task_count) OVER ( ORDER BY x.month) AS last_period_task_count \r\n   FROM current_period_task x\r\n)\r\nSELECT b.month, \r\n       b.current_period_task_count, \r\n       ROUND(((b.current_period_task_count - b.last_period_task_count ) \/ b.last_period_task_count),2) AS MoMChg\r\nFROM last_period_task AS b<\/pre>\n<ol>\n<li>The first CTE &#8211; &#8216;current_period_task&#8217; defines the query to retrieve a count for all activity of type Task group by the calendar month.<\/li>\n<li>The second CTE &#8211; &#8216;last_period_task&#8217; reads from the first CTE, and also leverage the window function LAG to return the task_count for the previous month. Note that the ORDER BY clause is critical for the LAG function to work.<\/li>\n<li>The main query reads from the second CTE &#8211; &#8216;last_period_task&#8217; and derive the month over month calculation.<\/li>\n<\/ol>\n<pre class=\"font:courier-new tab-convert:true lang:default decode:true \" title=\"Output:\">MoMChg\tcurrent_period_task_count\tmonth\r\n\t283\t\"2018-01-01\"\r\n-0.08\t260\t\"2018-02-01\"\r\n0.1\t287\t\"2018-03-01\"\r\n-0.08\t264\t\"2018-04-01\"\r\n0.11\t292\t\"2018-05-01\"\r\n0\t293\t\"2018-06-01\"\r\n-0.03\t285\t\"2018-07-01\"\r\n0\t285\t\"2018-08-01\"\r\n0\t284\t\"2018-09-01\"\r\n0\t283\t\"2018-10-01\"\r\n-0.05\t268\t\"2018-11-01\"\r\n0.06\t285\t\"2018-12-01\"\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Resources<\/h3>\n<ul>\n<li><i>Download<\/i>:\u00a0<a href=\"https:\/\/couchbase.com\/downloads?family=server&amp;product=couchbase-server-developer\">Download Couchbase Server 6.5<\/a><i><\/i><\/li>\n<li><i>Documentation<\/i>:\u00a0<a href=\"https:\/\/docs.couchbase.com\/server\/6.5\/introduction\/whats-new.html\">Couchbase Server 6.5 What\u2019s New<\/a><i><\/i><\/li>\n<li><a href=\"https:\/\/www.couchbase.com\/blog\/tag\/6-5\/\"><b>All 6.5 Blogs<\/b><\/a><\/li>\n<\/ul>\n<p>We would love to hear from you on how you liked the 6.5 features and how it\u2019ll benefit your business going forward. Please share your feedback via the comments or in the\u00a0<a href=\"https:\/\/www.couchbase.com\/forums\/\">forum<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whether it is a personal need to understand your spending pattern from looking at the bank transactions, or seeking to improve the sales performance in your organization by looking at the sales activities, the real insights are only visible with [&hellip;]<\/p>\n","protected":false},"author":26326,"featured_media":6547,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1814,1815,1819,1812],"tags":[2378,2306],"ppma_author":[8919],"class_list":["post-6502","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application-design","category-best-practices-and-tutorials","category-data-modeling","category-n1ql-query","tag-6-5","tag-analytical-functions"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Get a Bigger Picture with N1QL Window Functions and CTE<\/title>\n<meta name=\"description\" content=\"This article focuses on a couple of examples of how you can leverage N1QL Window functions and CTE to address two very common business questions.\" \/>\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.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get a Bigger Picture with N1QL Window Functions and CTE\" \/>\n<meta property=\"og:description\" content=\"This article focuses on a couple of examples of how you can leverage N1QL Window functions and CTE to address two very common business questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-12T16:31:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T00:22:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1334\" \/>\n\t<meta property=\"og:image:height\" content=\"346\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Binh Le\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binh Le\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\"},\"author\":{\"name\":\"Binh Le\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63\"},\"headline\":\"Get a Bigger Picture with N1QL Window Functions and CTE\",\"datePublished\":\"2019-03-12T16:31:53+00:00\",\"dateModified\":\"2025-06-14T00:22:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\"},\"wordCount\":496,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png\",\"keywords\":[\"6.5\",\"analytical functions\"],\"articleSection\":[\"Application Design\",\"Best Practices and Tutorials\",\"Data Modeling\",\"SQL++ \/ N1QL Query\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\",\"name\":\"Get a Bigger Picture with N1QL Window Functions and CTE\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png\",\"datePublished\":\"2019-03-12T16:31:53+00:00\",\"dateModified\":\"2025-06-14T00:22:10+00:00\",\"description\":\"This article focuses on a couple of examples of how you can leverage N1QL Window functions and CTE to address two very common business questions.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png\",\"width\":1334,\"height\":346},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get a Bigger Picture with N1QL Window Functions and CTE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63\",\"name\":\"Binh Le\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/5b68c37e30928a9d7b2c8470b1a303b7\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g\",\"caption\":\"Binh Le\"},\"description\":\"Binh Le is a Principal Product Manager for Couchbase Query service. Prior to Couchbase, he worked at Oracle and led the product management team for Sales Cloud Analytics and CRM OnDemand. Binh holds a Bachelor's Degree in Computer Science from the University of Brighton, UK.\",\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/binh-le-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Get a Bigger Picture with N1QL Window Functions and CTE","description":"This article focuses on a couple of examples of how you can leverage N1QL Window functions and CTE to address two very common business questions.","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.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/","og_locale":"en_US","og_type":"article","og_title":"Get a Bigger Picture with N1QL Window Functions and CTE","og_description":"This article focuses on a couple of examples of how you can leverage N1QL Window functions and CTE to address two very common business questions.","og_url":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/","og_site_name":"The Couchbase Blog","article_published_time":"2019-03-12T16:31:53+00:00","article_modified_time":"2025-06-14T00:22:10+00:00","og_image":[{"width":1334,"height":346,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png","type":"image\/png"}],"author":"Binh Le","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binh Le","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/"},"author":{"name":"Binh Le","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63"},"headline":"Get a Bigger Picture with N1QL Window Functions and CTE","datePublished":"2019-03-12T16:31:53+00:00","dateModified":"2025-06-14T00:22:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/"},"wordCount":496,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png","keywords":["6.5","analytical functions"],"articleSection":["Application Design","Best Practices and Tutorials","Data Modeling","SQL++ \/ N1QL Query"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/","url":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/","name":"Get a Bigger Picture with N1QL Window Functions and CTE","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png","datePublished":"2019-03-12T16:31:53+00:00","dateModified":"2025-06-14T00:22:10+00:00","description":"This article focuses on a couple of examples of how you can leverage N1QL Window functions and CTE to address two very common business questions.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2019\/03\/Screen-Shot-2019-03-11-at-9.50.05-AM.png","width":1334,"height":346},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/get-a-bigger-picture-with-n1ql-window-functions-and-cte\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Get a Bigger Picture with N1QL Window Functions and CTE"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63","name":"Binh Le","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/5b68c37e30928a9d7b2c8470b1a303b7","url":"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g","caption":"Binh Le"},"description":"Binh Le is a Principal Product Manager for Couchbase Query service. Prior to Couchbase, he worked at Oracle and led the product management team for Sales Cloud Analytics and CRM OnDemand. Binh holds a Bachelor's Degree in Computer Science from the University of Brighton, UK.","url":"https:\/\/www.couchbase.com\/blog\/author\/binh-le-2\/"}]}},"authors":[{"term_id":8919,"user_id":26326,"is_guest":0,"slug":"binh-le-2","display_name":"Binh Le","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g","author_category":"","last_name":"Le","first_name":"Binh","job_title":"","user_url":"","description":"Binh Le is a Principal Product Manager for Couchbase Query service. Prior to Couchbase, he worked at Oracle and led the product management team for Sales Clould Analytics and CRM OnDemand. Binh holds a Bachelor's Degree in Computer Science from the University of Brighton, UK."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/6502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/26326"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=6502"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/6502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/6547"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=6502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=6502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=6502"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=6502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}