Back to Blog
Postgres lag function6/23/2023 Similar to an aggregate function, a window function operates on a set of rows. Group_name Code language: SQL (Structured Query Language) ( sql )Īs you see clearly from the output, the AVG() function reduces the number of rows returned by the queries in both examples. INNER JOIN product_groups USING ( group_id) The following example returns the average price for every product group. To apply the aggregate function to subsets of rows, you use the GROUP BY clause. SELECT AVG (price)Ĭode language: SQL (Structured Query Language) ( sql ) The following example uses the AVG() aggregate function to calculate the average price of all products in the products table. An aggregate function aggregates data from a set of rows into a single row. The easiest way to understand the window functions is to start by reviewing the aggregate functions. ( 'Samsung Galaxy Tab', 3, 200) Code language: SQL (Structured Query Language) ( sql ) Introduction to PostgreSQL window functions INSERT INTO products (product_name, group_id,price) Second, insert some rows into these tables: INSERT INTO product_groups (group_name) ) Code language: SQL (Structured Query Language) ( sql ) Setting up sample tablesįirst, create two tables named products and product_groups for the demonstration: CREATE TABLE product_groups (įOREIGN KEY ( group_id) REFERENCES product_groups ( group_id) Summary: in this tutorial, you will learn how to use the PostgreSQL window functions to perform the calculation across a set of rows related to the current row.
0 Comments
Read More
Leave a Reply. |