diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-09-23 20:19:44 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-09-23 20:19:44 +0200 |
| commit | 20219dd4f7192c61492b7667dd3f15b05dbba044 (patch) | |
| tree | 14d436e34d52ddea4517bb2c2ed8de9fe414e609 /content/posts/postgres-aggregates/sequential-ctes.sql | |
| parent | 7455ccba6c2e42006ecb606a3043d4407a2a774e (diff) | |
feat: Add post to version control
Diffstat (limited to 'content/posts/postgres-aggregates/sequential-ctes.sql')
| -rw-r--r-- | content/posts/postgres-aggregates/sequential-ctes.sql | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/content/posts/postgres-aggregates/sequential-ctes.sql b/content/posts/postgres-aggregates/sequential-ctes.sql new file mode 100644 index 0000000..fa18acd --- /dev/null +++ b/content/posts/postgres-aggregates/sequential-ctes.sql @@ -0,0 +1,28 @@ +-- EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS) +WITH product_with_reviews AS ( + SELECT + products.id AS product_id, + products.name AS product_name, + COUNT(reviews.id) AS num_reviews + FROM + products + LEFT JOIN reviews ON reviews.product_id = products.id + -- WHERE + -- products.id % 10 = 0 + GROUP BY + products.id, + products.name +) +SELECT + product_with_reviews.product_id, + product_name, + num_reviews, + COUNT(id) AS num_orders +FROM + product_with_reviews + LEFT JOIN orders ON orders.product_id = product_with_reviews.product_id +GROUP BY + product_with_reviews.product_id, + product_name, + num_reviews; + |
