aboutsummaryrefslogtreecommitdiff
path: root/content/posts/postgres-aggregates/aggregation.sql
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/postgres-aggregates/aggregation.sql')
-rw-r--r--content/posts/postgres-aggregates/aggregation.sql20
1 files changed, 20 insertions, 0 deletions
diff --git a/content/posts/postgres-aggregates/aggregation.sql b/content/posts/postgres-aggregates/aggregation.sql
new file mode 100644
index 0000000..ebb9ed5
--- /dev/null
+++ b/content/posts/postgres-aggregates/aggregation.sql
@@ -0,0 +1,20 @@
+EXPLAIN (
+ ANALYZE,
+ COSTS,
+ VERBOSE,
+ BUFFERS
+)
+SELECT
+ products.id AS product_id,
+ products.name AS product_name,
+ COUNT(DISTINCT reviews.id) AS num_reviews,
+ COUNT(DISTINCT orders.id) AS num_orders
+FROM
+ products
+ LEFT JOIN reviews ON reviews.product_id = products.id
+ LEFT JOIN orders ON orders.product_id = products.id
+ -- WHERE
+ -- products.id % 10 = 0
+GROUP BY
+ products.id;
+