{"id":1263,"date":"2024-12-25T08:38:55","date_gmt":"2024-12-25T01:38:55","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1263"},"modified":"2025-01-05T21:30:05","modified_gmt":"2025-01-05T14:30:05","slug":"postgresql-sum-window-function","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/","title":{"rendered":"PostgreSQL SUM Window Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>SUM()<\/code> window function to calculate the sum of values within a partition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-sum-window-function'>Getting Started with the PostgreSQL SUM window function <a href=\"#getting-started-with-the-postgresql-sum-window-function\" class=\"anchor\" id=\"getting-started-with-the-postgresql-sum-window-function\" title=\"Anchor for Getting Started with the PostgreSQL SUM window function\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, the <code>SUM()<\/code> window function returns the sum of values within a partition. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>SUM()<\/code> window function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">SUM(<span class=\"hljs-keyword\">value<\/span>) <span class=\"hljs-keyword\">OVER<\/span> (\n    <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> partition_expression,\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> order_expression\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>: A column or expression from which you want to calculate the sum of values.<\/li>\n\n\n\n<li><code>OVER<\/code>: Defines the window frame where the <code>SUM()<\/code> function applies.<\/li>\n\n\n\n<li><code>PARTITION BY<\/code>: Divides the result set into partitions by the <code>partition_expression<\/code>. The <code>partition_expression<\/code> can be one or more columns. If you skip the <code>PARTITION BY<\/code> clause, the <code>SUM()<\/code> function treats the whole result set as a single partition.<\/li>\n\n\n\n<li><code>ORDER BY<\/code>: Determines the order of rows within each partition.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-sum-window-function-examples'>PostgreSQL SUM Window Function Examples <a href=\"#postgresql-sum-window-function-examples\" class=\"anchor\" id=\"postgresql-sum-window-function-examples\" title=\"Anchor for PostgreSQL SUM Window Function Examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take examples of using the <code>SUM()<\/code> window function with the <code>inventories<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"139\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png\" alt=\"PostgreSQL SUM window function - Sample Table\" class=\"wp-image-1046\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='calculating-the-total-inventory-quantity'>Calculating the Total Inventory Quantity <a href=\"#calculating-the-total-inventory-quantity\" class=\"anchor\" id=\"calculating-the-total-inventory-quantity\" title=\"Anchor for Calculating the Total Inventory Quantity\">#<\/a><\/h3>\n\n\n\n<p>The following statement uses the <code>SUM<\/code> window function to retrieve the inventory of a product along with the total quantity:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  inventory_id,\n  product_id,\n  quantity,\n  SUM(quantity) <span class=\"hljs-keyword\">OVER<\/span> () <span class=\"hljs-keyword\">AS<\/span> total_quantity\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGludmVudG9yeV9pZCwgcHJvZHVjdF9pZCwgcXVhbnRpdHksIFNVTShxdWFudGl0eSkgT1ZFUiAoKSBBUyB0b3RhbF9xdWFudGl0eSBGUk9NIGludmVudG9yaWVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> inventory_id | product_id | quantity | total_quantity\n--------------+------------+----------+----------------\n            1 |          1 |      100 |           5360\n            2 |          2 |      150 |           5360\n            3 |          3 |      200 |           5360\n            4 |          4 |      120 |           5360\n            5 |          5 |      130 |           5360\n            6 |          6 |      110 |           5360\n            7 |          7 |      140 |           5360\n            8 |          8 |      160 |           5360\n            9 |          9 |      170 |           5360\n           10 |         10 |      180 |           5360\n           11 |         11 |      190 |           5360\n           12 |         12 |      200 |           5360\n           13 |         13 |      210 |           5360\n           14 |         14 |      220 |           5360\n           15 |         15 |      230 |           5360\n           16 |         16 |      240 |           5360\n           17 |         17 |      250 |           5360\n           18 |         18 |      260 |           5360\n           19 |         19 |      270 |           5360\n           20 |         20 |      280 |           5360\n           21 |         21 |      290 |           5360\n           22 |         22 |      300 |           5360\n           23 |         23 |      310 |           5360\n           24 |         24 |      320 |           5360\n           25 |         25 |      330 |           5360<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, <code>SUM(quantity) OVER ()<\/code> returns the sum of all quantity values in the <code>inventories<\/code> table.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id='calculating-the-total-inventory-quantity-for-each-warehouse'>Calculating the Total Inventory Quantity for Each Warehouse <a href=\"#calculating-the-total-inventory-quantity-for-each-warehouse\" class=\"anchor\" id=\"calculating-the-total-inventory-quantity-for-each-warehouse\" title=\"Anchor for Calculating the Total Inventory Quantity for Each Warehouse\">#<\/a><\/h3>\n\n\n\n<p>The following <code>SELECT<\/code> statement uses the <code>SUM<\/code> window function to retrieve the inventory of products along with the total quantity for each warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> \n  inventory_id,\n  product_id,\n  quantity,\n  SUM(quantity) <span class=\"hljs-keyword\">OVER<\/span> (\n       <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> warehouse_id\n  ) <span class=\"hljs-keyword\">AS<\/span> total_quantity\n<span class=\"hljs-keyword\">FROM<\/span> \n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGludmVudG9yeV9pZCwgcHJvZHVjdF9pZCwgcXVhbnRpdHksIFNVTShxdWFudGl0eSkgT1ZFUiAoIFBBUlRJVElPTiBCWSB3YXJlaG91c2VfaWQgKSBBUyB0b3RhbF9xdWFudGl0eSBGUk9NIGludmVudG9yaWVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> inventory_id | product_id | quantity | total_quantity\n--------------+------------+----------+----------------\n           25 |         25 |      330 |           1890\n            4 |          4 |      120 |           1890\n            7 |          7 |      140 |           1890\n           10 |         10 |      180 |           1890\n           13 |         13 |      210 |           1890\n           16 |         16 |      240 |           1890\n           19 |         19 |      270 |           1890\n           22 |         22 |      300 |           1890\n            1 |          1 |      100 |           1890\n           20 |         20 |      280 |           1690\n           11 |         11 |      190 |           1690\n           14 |         14 |      220 |           1690\n           17 |         17 |      250 |           1690\n            2 |          2 |      150 |           1690\n            5 |          5 |      130 |           1690\n           23 |         23 |      310 |           1690\n            8 |          8 |      160 |           1690\n            6 |          6 |      110 |           1780\n            3 |          3 |      200 |           1780\n           12 |         12 |      200 |           1780\n           21 |         21 |      290 |           1780\n           18 |         18 |      260 |           1780\n           15 |         15 |      230 |           1780\n           24 |         24 |      320 |           1780\n            9 |          9 |      170 |           1780<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PARTITION BY<\/code>: Divides the rows in the <code>inventories<\/code> table into partitions by <code>warehouse_id<\/code>.<\/li>\n\n\n\n<li><code>SUM(quantity) OVER (PARTITION BY warehouse_id)<\/code> returns the sum of inventory quantity within each warehouse.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='calculating-running-total'>Calculating Running Total <a href=\"#calculating-running-total\" class=\"anchor\" id=\"calculating-running-total\" title=\"Anchor for Calculating Running Total\">#<\/a><\/h3>\n\n\n\n<p>The following <code>SELECT<\/code> statement uses the <code>SUM()<\/code> window function to retrieve the inventory of a product along with the running total for each warehouse:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> \n  inventory_id,\n  product_id,\n  warehouse_id,\n  quantity,\n  SUM(quantity) <span class=\"hljs-keyword\">OVER<\/span> (\n     <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">BY<\/span> warehouse_id \n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> product_id\n  ) <span class=\"hljs-keyword\">AS<\/span> cumulative_quantity\n<span class=\"hljs-keyword\">FROM<\/span> \n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGludmVudG9yeV9pZCwgcHJvZHVjdF9pZCwgcXVhbnRpdHksIFNVTShxdWFudGl0eSkgT1ZFUiAoIFBBUlRJVElPTiBCWSB3YXJlaG91c2VfaWQgKSBBUyB0b3RhbF9xdWFudGl0eSBGUk9NIGludmVudG9yaWVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> inventory_id | product_id | warehouse_id | quantity | cumulative_quantity\n--------------+------------+--------------+----------+---------------------\n            1 |          1 |            1 |      100 |                 100\n            4 |          4 |            1 |      120 |                 220\n            7 |          7 |            1 |      140 |                 360\n           10 |         10 |            1 |      180 |                 540\n           13 |         13 |            1 |      210 |                 750\n           16 |         16 |            1 |      240 |                 990\n           19 |         19 |            1 |      270 |                1260\n           22 |         22 |            1 |      300 |                1560\n           25 |         25 |            1 |      330 |                1890\n            2 |          2 |            2 |      150 |                 150\n            5 |          5 |            2 |      130 |                 280\n            8 |          8 |            2 |      160 |                 440\n           11 |         11 |            2 |      190 |                 630\n           14 |         14 |            2 |      220 |                 850\n           17 |         17 |            2 |      250 |                1100\n           20 |         20 |            2 |      280 |                1380\n           23 |         23 |            2 |      310 |                1690\n            3 |          3 |            3 |      200 |                 200\n            6 |          6 |            3 |      110 |                 310\n            9 |          9 |            3 |      170 |                 480\n           12 |         12 |            3 |      200 |                 680\n           15 |         15 |            3 |      230 |                 910\n           18 |         18 |            3 |      260 |                1170\n           21 |         21 |            3 |      290 |                1460\n           24 |         24 |            3 |      320 |                1780<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PARTITION BY<\/code>: Divides the rows by <code>warehouse_id<\/code>.<\/li>\n\n\n\n<li><code>ORDER BY<\/code>: Sorts the rows within each partition by <code>product_id<\/code>.<\/li>\n\n\n\n<li><code>SUM<\/code> returns a running total of quantity within each warehouse, ordered by <code>product_id<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PostgreSQL <code>SUM<\/code> window function to calculate the sum of values within a partition.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=sum-window-function\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1263\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL SUM Window Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1263\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL SUM Window Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition. Getting Started with the PostgreSQL SUM window function # In PostgreSQL, the SUM() window function returns the sum of values within a partition. Here&#8217;s the syntax of the SUM() window function: In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1072,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1263","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL SUM Window Function<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition.\" \/>\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.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL SUM Window Function\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-05T14:30:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png\" \/>\n\t<meta property=\"og:image:width\" content=\"144\" \/>\n\t<meta property=\"og:image:height\" content=\"139\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/\",\"name\":\"PostgreSQL SUM Window Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/inventories.png\",\"datePublished\":\"2024-12-25T01:38:55+00:00\",\"dateModified\":\"2025-01-05T14:30:05+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/inventories.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/inventories.png\",\"width\":144,\"height\":139},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/postgresql-sum-window-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Window Functions\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-window-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL SUM Window Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL SUM Window Function","description":"In this tutorial, you'll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition.","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.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL SUM Window Function","og_description":"In this tutorial, you'll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-05T14:30:05+00:00","og_image":[{"width":144,"height":139,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/","url":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/","name":"PostgreSQL SUM Window Function","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","datePublished":"2024-12-25T01:38:55+00:00","dateModified":"2025-01-05T14:30:05+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL SUM() window function to calculate the sum of values within a partition.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","width":144,"height":139},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/postgresql-sum-window-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Window Functions","item":"https:\/\/www.pgtutorial.com\/postgresql-window-functions\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL SUM Window Function"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1263"}],"version-history":[{"count":3,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1263\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1263\/revisions\/1480"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1072"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}