{"id":2640,"date":"2025-03-11T23:49:15","date_gmt":"2025-03-11T15:49:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2640"},"modified":"2025-03-11T23:49:15","modified_gmt":"2025-03-11T15:49:15","slug":"postgresql-group-by-%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/11\/postgresql-group-by-%e8%af%ad%e5%8f%a5\/","title":{"rendered":"PostgreSQL GROUP BY \u8bed\u53e5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 PostgreSQL \u4e2d\uff0c<code>GROUP BY<\/code> \u8bed\u53e5\u7528\u4e8e\u5c06\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u6570\u636e\u6309\u6307\u5b9a\u7684\u5217\u8fdb\u884c\u5206\u7ec4\u3002\u7ed3\u5408\u805a\u5408\u51fd\u6570\uff08\u5982 <code>COUNT()<\/code>\u3001<code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>MIN()<\/code>\u3001<code>MAX()<\/code> \u7b49\uff09\u4f7f\u7528\uff0c<code>GROUP BY<\/code> \u53ef\u4ee5\u5e2e\u52a9\u4f60\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u7684\u7ed3\u679c\u8fdb\u884c\u8ba1\u7b97\u6216\u6c47\u603b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong><code>GROUP BY<\/code> \u5b50\u53e5\u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, aggregate_function(column3)\nFROM table_name\nWHERE condition\nGROUP BY column1, column2;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>column1, column2, ...<\/code><\/strong>: \u7528\u4e8e\u5206\u7ec4\u7684\u5217\u3002<\/li>\n\n\n\n<li><strong><code>aggregate_function(column3)<\/code><\/strong>: \u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u503c\u3002<\/li>\n\n\n\n<li><strong><code>WHERE<\/code><\/strong>: \u53ef\u9009\u7684\u8fc7\u6ee4\u6761\u4ef6\u3002<\/li>\n\n\n\n<li><strong><code>GROUP BY<\/code><\/strong>: \u7528\u4e8e\u6307\u5b9a\u6309\u54ea\u4e9b\u5217\u8fdb\u884c\u5206\u7ec4\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong><code>GROUP BY<\/code> \u7684\u57fa\u672c\u793a\u4f8b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>sales<\/code> \u7684\u8868\uff0c\u5305\u542b <code>product_id<\/code>\u3001<code>amount<\/code> \u548c <code>sale_date<\/code> \u7b49\u5b57\u6bb5\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f7f\u7528 <code>GROUP BY<\/code> \u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 <strong>\u6309\u5355\u4e2a\u5217\u5206\u7ec4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u4ea7\u54c1ID\u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\nSELECT product_id, SUM(amount) \nFROM sales\nGROUP BY product_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u67e5\u8be2\u6839\u636e <code>product_id<\/code> \u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u9500\u552e\u603b\u989d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 <strong>\u6309\u591a\u4e2a\u5217\u5206\u7ec4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u4ea7\u54c1ID\u548c\u9500\u552e\u65e5\u671f\u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u6bcf\u5929\u7684\u603b\u9500\u552e\u989d\nSELECT product_id, sale_date, SUM(amount) \nFROM sales\nGROUP BY product_id, sale_date;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\uff0c\u67e5\u8be2\u6839\u636e <code>product_id<\/code> \u548c <code>sale_date<\/code> \u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u5728\u6bcf\u5929\u7684\u603b\u9500\u552e\u989d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u5e38\u7528\u7684\u805a\u5408\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u5e38\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u4fbf\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u603b\u7ed3\u8ba1\u7b97\u3002\u5e38\u7528\u7684\u805a\u5408\u51fd\u6570\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>COUNT()<\/code><\/strong>: \u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u7684\u8bb0\u5f55\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  -- \u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u9500\u552e\u8bb0\u5f55\u6570\n  SELECT product_id, COUNT(*) \n  FROM sales\n  GROUP BY product_id;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>SUM()<\/code><\/strong>: \u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u67d0\u5217\u7684\u603b\u548c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  -- \u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\n  SELECT product_id, SUM(amount)\n  FROM sales\n  GROUP BY product_id;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>AVG()<\/code><\/strong>: \u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u67d0\u5217\u7684\u5e73\u5747\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  -- \u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u5e73\u5747\u9500\u552e\u989d\n  SELECT product_id, AVG(amount)\n  FROM sales\n  GROUP BY product_id;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>MIN()<\/code><\/strong>: \u83b7\u53d6\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u67d0\u5217\u7684\u6700\u5c0f\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  -- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7684\u6700\u4f4e\u9500\u552e\u989d\n  SELECT product_id, MIN(amount)\n  FROM sales\n  GROUP BY product_id;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>MAX()<\/code><\/strong>: \u83b7\u53d6\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u67d0\u5217\u7684\u6700\u5927\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  -- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7684\u6700\u9ad8\u9500\u552e\u989d\n  SELECT product_id, MAX(amount)\n  FROM sales\n  GROUP BY product_id;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\u8fdb\u884c\u8fc7\u6ee4<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u8fc7\u6ee4\u5206\u7ec4\u540e\u7684\u7ed3\u679c\uff0c\u7c7b\u4f3c\u4e8e <code>WHERE<\/code> \u5b50\u53e5\uff0c\u4f46 <code>WHERE<\/code> \u8fc7\u6ee4\u7684\u662f\u539f\u59cb\u6570\u636e\uff0c\u800c <code>HAVING<\/code> \u8fc7\u6ee4\u7684\u662f\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u53ea\u5305\u542b\u603b\u9500\u552e\u989d\u8d85\u8fc7 1000 \u7684\u4ea7\u54c1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u9500\u552e\u603b\u989d\u5927\u4e8e 1000 \u7684\u4ea7\u54c1\nSELECT product_id, SUM(amount)\nFROM sales\nGROUP BY product_id\nHAVING SUM(amount) &gt; 1000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u901a\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u5b83\u5141\u8bb8\u4f60\u57fa\u4e8e\u805a\u5408\u503c\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong><code>GROUP BY<\/code> \u4e0e <code>ORDER BY<\/code> \u7ed3\u5408\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\uff0c<code>GROUP BY<\/code> \u5b50\u53e5\u4e0e <code>ORDER BY<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\uff0c\u5e76\u6309\u9500\u552e\u989d\u964d\u5e8f\u6392\u5217\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u603b\u9500\u552e\u989d\u964d\u5e8f\u6392\u5217\u6bcf\u4e2a\u4ea7\u54c1\u7684\u9500\u552e\u989d\nSELECT product_id, SUM(amount)\nFROM sales\nGROUP BY product_id\nORDER BY SUM(amount) DESC;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong><code>GROUP BY<\/code> \u4e0e <code>JOIN<\/code> \u4e00\u8d77\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u4e5f\u53ef\u4ee5\u4e0e <code>JOIN<\/code> \u7ed3\u5408\u4f7f\u7528\uff0c\u5141\u8bb8\u4f60\u5728\u8fde\u63a5\u591a\u4e2a\u8868\u540e\u8fdb\u884c\u5206\u7ec4\u548c\u805a\u5408\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868\uff1a<code>products<\/code>\uff08\u5305\u542b\u4ea7\u54c1\u4fe1\u606f\uff09\u548c <code>sales<\/code>\uff08\u5305\u542b\u9500\u552e\u6570\u636e\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u4ea7\u54c1\u7c7b\u522b\u5206\u7ec4\u5e76\u8ba1\u7b97\u9500\u552e\u603b\u989d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7c7b\u522b\u7684\u603b\u9500\u552e\u989d\nSELECT p.category, SUM(s.amount)\nFROM products p\nJOIN sales s ON p.product_id = s.product_id\nGROUP BY p.category;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. <strong>\u4f7f\u7528\u8868\u8fbe\u5f0f\u8fdb\u884c\u5206\u7ec4<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u4f7f\u7528\u5217\u540d\u5916\uff0c<code>GROUP BY<\/code> \u8fd8\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0f\u8fdb\u884c\u5206\u7ec4\u3002\u4f8b\u5982\uff0c\u6309\u5e74\u4efd\u5206\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u9500\u552e\u5e74\u4efd\u5206\u7ec4\uff0c\u8ba1\u7b97\u6bcf\u5e74\u603b\u9500\u552e\u989d\nSELECT EXTRACT(YEAR FROM sale_date) AS sale_year, SUM(amount)\nFROM sales\nGROUP BY EXTRACT(YEAR FROM sale_date);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8. <strong><code>GROUP BY<\/code> \u548c <code>DISTINCT<\/code> \u7684\u7ed3\u5408\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> \u53ef\u4ee5\u4e0e <code>DISTINCT<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u6765\u5bf9\u4e0d\u540c\u7684\u503c\u8fdb\u884c\u5206\u7ec4\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7c7b\u522b\u7684\u4e0d\u540c\u9500\u552e\u91d1\u989d\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u83b7\u53d6\u6bcf\u4e2a\u4ea7\u54c1\u7c7b\u522b\u7684\u4e0d\u540c\u9500\u552e\u91d1\u989d\u7684\u6570\u91cf\nSELECT category, COUNT(DISTINCT amount)\nFROM products\nJOIN sales ON products.product_id = sales.product_id\nGROUP BY category;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">9. <strong>\u6027\u80fd\u8003\u8651<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>GROUP BY<\/code> \u8bed\u53e5\u65f6\uff0c\u6570\u636e\u5e93\u4f1a\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u548c\u805a\u5408\uff0c\u56e0\u6b64\u9700\u8981\u5bf9\u5206\u7ec4\u5217\u8fdb\u884c\u6392\u5e8f\u6216\u6563\u5217\u64cd\u4f5c\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u5bf9\u5206\u7ec4\u5217\u521b\u5efa\u7d22\u5f15\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8be2\u7684\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">10. <strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>GROUP BY<\/code> \u7528\u4e8e\u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\u3002<\/li>\n\n\n\n<li><code>GROUP BY<\/code> \u5e38\u5e38\u4e0e\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u7edf\u8ba1\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u4f7f\u7528 <code>HAVING<\/code> \u5b50\u53e5\u5728\u5206\u7ec4\u540e\u8fc7\u6ee4\u6570\u636e\uff0c<code>ORDER BY<\/code> \u5b50\u53e5\u5bf9\u5206\u7ec4\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n\n\n\n<li><code>GROUP BY<\/code> \u4e5f\u53ef\u4ee5\u4e0e <code>JOIN<\/code> \u548c\u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u67e5\u8be2\u9700\u6c42\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1 <code>GROUP BY<\/code> \u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6709\u6548\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u6c47\u603b\u548c\u5206\u6790\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u80fd\u591f\u63d0\u4f9b\u66f4\u6df1\u5165\u7684\u6d1e\u5bdf\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0cGROUP BY \u8bed\u53e5\u7528\u4e8e\u5c06\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u6570\u636e\u6309\u6307\u5b9a\u7684\u5217\u8fdb\u884c\u5206\u7ec4\u3002\u7ed3\u5408\u805a\u5408\u51fd\u6570\uff08\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2640","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2640"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2640\/revisions"}],"predecessor-version":[{"id":2642,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2640\/revisions\/2642"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2641"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}