{"id":1870,"date":"2025-07-29T09:31:05","date_gmt":"2025-07-29T00:31:05","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/?post_type=route&#038;p=1870"},"modified":"2026-01-29T09:31:19","modified_gmt":"2026-01-29T00:31:19","slug":"group-by%e4%bd%bf%e3%81%84%e6%96%b9%e5%ae%8c%e5%85%a8%e8%a7%a3%e8%aa%ac%ef%bd%9csql%e3%82%a8%e3%83%a9%e3%83%bc%e3%83%bbwhere-having%e3%81%ae%e9%81%95%e3%81%84%e3%81%af%ef%bc%9f","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/1870\/","title":{"rendered":"GROUP BY\u4f7f\u3044\u65b9\u5b8c\u5168\u89e3\u8aac\uff5cSQL\u30a8\u30e9\u30fc\u30fbWHERE HAVING\u306e\u9055\u3044\u306f\uff1f"},"content":{"rendered":"<div>\n<h2>GROUP BY\u3067\u8ff7\u308f\u306a\u3044\u2500\u2500WHERE\u3068HAVING\u3001\u3069\u3061\u3089\u3067\u7d5e\u308b\uff1f\u73fe\u5834\u3067\u5dee\u304c\u3064\u304f\u201c\u5b9f\u884c\u9806\u5e8f\u201d\u7406\u89e3\u6cd5<\/h2>\n<p>\n    <strong>GROUP BY<\/strong>\u3067\u96c6\u8a08\u3057\u3088\u3046\u3068\u3057\u305f\u3089\u3001\u30a8\u30e9\u30fc\uff1f\u601d\u3063\u305f\u5024\u304c\u51fa\u306a\u3044\uff1f<br \/>\n    \u2026\u305d\u3093\u306a\u201c\u3064\u307e\u305a\u304d\u201d\u306b\u3001\u79c1\u305f\u3061\u306f\u4f55\u5ea6\u3082\u51fa\u304f\u308f\u3057\u307e\u3059\u3002<br \/>\n    \u306a\u305c<strong>WHERE<\/strong>\u3068<strong>HAVING<\/strong>\u306e\u4f7f\u3044\u5206\u3051\u3067\u8ff7\u3046\u306e\u304b\uff1f<br \/>\n    \u2500\u2500\u7b54\u3048\u306f\u300c<strong>SQL\u306e\u5b9f\u884c\u9806\u5e8f<\/strong>\u300d\u3092\u201c\u8179\u843d\u3061\u201d\u3067\u7406\u89e3\u3067\u304d\u3066\u3044\u306a\u3044\u304b\u3089\u3067\u3059\u3002<br \/>\n    \u79c1\u305f\u3061\u958b\u767a\u8005\u304c\u73fe\u5834\u3067\u30cf\u30de\u308b\u7406\u7531\u3068\u3001\u660e\u65e5\u304b\u3089\u4f7f\u3048\u308b\u201c\u8131\u843d\u3057\u306a\u3044GROUP BY\u201d\u30c6\u30af\u3092\u56f3\u89e3\u3068\u5171\u306b\u89e3\u8aac\u3057\u307e\u3059\u3002\n  <\/p>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aGROUP BY<\/strong><br \/>\n      SQL\u3067\u30c7\u30fc\u30bf\u3092\u7279\u5b9a\u306e\u5217\u3054\u3068\u306b\u307e\u3068\u3081\u3066\u96c6\u8a08\u3059\u308b\u305f\u3081\u306e\u69cb\u6587\u3002\u58f2\u4e0a\u3084\u4ef6\u6570\u306a\u3069\u3001\u30b0\u30eb\u30fc\u30d7\u5358\u4f4d\u3067\u8a08\u7b97\u3057\u305f\u3044\u3068\u304d\u306b\u4f7f\u3046\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aWHERE<\/strong><br \/>\n      SQL\u3067\u30c7\u30fc\u30bf\u3092\u62bd\u51fa\u30fb\u7d5e\u308a\u8fbc\u307f\u3059\u308b\u305f\u3081\u306e\u6761\u4ef6\u6307\u5b9a\u53e5\u3002\u30b0\u30eb\u30fc\u30d7\u5316\u3084\u96c6\u8a08\u306e\u524d\u6bb5\u968e\u3067\u4f7f\u3046\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aHAVING<\/strong><br \/>\n      GROUP BY\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u30fb\u96c6\u8a08\u3057\u305f\u5f8c\u306e\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u3001\u3055\u3089\u306b\u7d5e\u308a\u8fbc\u3080\u305f\u3081\u306e\u53e5\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aSQL<\/strong><br \/>\n      \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306e\u8a00\u8a9e\u3002\u30c7\u30fc\u30bf\u306e\u691c\u7d22\u30fb\u96c6\u8a08\u30fb\u66f4\u65b0\u30fb\u524a\u9664\u306a\u3069\u3092\u884c\u3046\u3002\n    <\/p>\n<p>\n      \uff08SQL\u306e\u57fa\u672c\u3084SELECT\u6587\u306e\u5b9f\u884c\u9806\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1233\/\" target=\"_blank\" rel=\"noopener\">\u521d\u5fc3\u8005\u5411\u3051SQL\u8b1b\u5ea7\uff5cSELECT\u6587\u306e\u66f8\u304d\u65b9\u3068\u5b9f\u884c\u9806\u3092\u5b8c\u5168\u89e3\u8aac<\/a\n      >\u300f\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09\n    <\/p>\n<\/blockquote>\n<hr \/>\n<h2>GROUP BY\u3068\u306f\uff1f\u2015\u2015\u751f\u30c7\u30fc\u30bf\u3092\u201c\u307e\u3068\u3081\u3066\u8981\u7d04\u201d\u3059\u308bSQL\u306e\u7b2c\u4e00\u6b69<\/h2>\n<p>\n    <strong>GROUP BY<\/strong>\u306f\u300c<strong>\u7279\u5b9a\u306e\u5217\u3054\u3068\u306b\u30c7\u30fc\u30bf\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3001\u96c6\u8a08\u3059\u308b<\/strong>\u300d\u305f\u3081\u306eSQL\u53e5\u3067\u3059\u3002<br \/>\n    \u305f\u3068\u3048\u3070\u3001\u4e0b\u8a18\u306e<strong>\u58f2\u4e0a\u30c6\u30fc\u30d6\u30eb<\/strong>\u3092\u4f8b\u306b\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002\n  <\/p>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1a\u30c6\u30fc\u30d6\u30eb<\/strong><br \/>\n      \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5185\u3067\u30c7\u30fc\u30bf\u3092\u884c\u3068\u5217\u3067\u7ba1\u7406\u3059\u308b\u305f\u3081\u306e\u8868\u3002Excel\u306e\u8868\u306e\u3088\u3046\u306a\u30a4\u30e1\u30fc\u30b8\u3002\n    <\/p>\n<\/blockquote>\n<pre><code>\r\n| order_id | order_date | product_category | total_price |\r\n|----------|------------|------------------|-------------|\r\n| 1        | 2023-04-01 | \u66f8\u7c4d             | 2000        |\r\n| 2        | 2023-04-02 | \u5bb6\u96fb             | 50000       |\r\n| 3        | 2023-04-03 | \u66f8\u7c4d             | 1500        |\r\n| 4        | 2023-05-10 | \u98df\u54c1             | 500         |\r\n| 5        | 2023-05-12 | \u5bb6\u96fb             | 30000       |\r\n<\/code><\/pre>\n<p>\n    \u3053\u306e\u307e\u307e\u3067\u306f\u300c\u30ab\u30c6\u30b4\u30ea\u5225\u306e\u58f2\u4e0a\u5408\u8a08\u300d\u304c\u898b\u3048\u307e\u305b\u3093\u3002<br \/>\n    <strong>GROUP BY<\/strong>\u306a\u3089\u3001\u300c\u66f8\u7c4d\u300d\u300c\u5bb6\u96fb\u300d\u2026\u306e\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306b\u307e\u3068\u3081\u3001<strong>\u5408\u8a08\u30fb\u4ef6\u6570<\/strong>\u306a\u3069\u3092\u3059\u3050\u7b97\u51fa\u3067\u304d\u307e\u3059\u3002\n  <\/p>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1a\u96c6\u8a08\u95a2\u6570\uff08SUM, COUNT\u306a\u3069\uff09<\/strong><br \/>\n      \u30c7\u30fc\u30bf\u306e\u5408\u8a08\uff08SUM\uff09\u3001\u4ef6\u6570\uff08COUNT\uff09\u3001\u5e73\u5747\uff08AVG\uff09\u306a\u3069\u3092\u8a08\u7b97\u3059\u308bSQL\u306e\u95a2\u6570\u3002GROUP BY\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u4f7f\u3046\u3002\n    <\/p>\n<p>\n      \uff08SQL\u306e\u96c6\u8a08\u95a2\u6570\u3084NULL\u306e\u6271\u3044\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1764\/\" target=\"_blank\" rel=\"noopener\">SQL NULL\u5b8c\u5168\u653b\u7565\uff5cIS NULL\u30fbCOALESCE\u306e\u4f7f\u3044\u5206\u3051\u3068DB\u5225\u30d0\u30b0\u9632\u6b62\u88535\u9078<\/a\n      >\u300f\u3082\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09\n    <\/p>\n<\/blockquote>\n<blockquote>\n<p>\n      <strong\n        >\u201c\u30d0\u30e9\u30d0\u30e9\u306a\u30c7\u30fc\u30bf\u3092\u201c\u610f\u5473\u3042\u308b\u304b\u305f\u307e\u308a\u201d\u306b\u6574\u7406\u3057\u3001\u96c6\u8a08\u3059\u308b\u201d<br \/>\n        \u3053\u308c\u304cGROUP BY\u306e\u672c\u8cea\u3067\u3059\u3002<\/strong\n      >\n    <\/p>\n<\/blockquote>\n<hr \/>\n<h2>1. \u306a\u305c\u6df7\u4e71\uff1fWHERE\u3068HAVING\u306e\u9055\u3044\u306f\u300cSQL\u306e\u51e6\u7406\u9806\u300d\u306b\u3042\u308a<\/h2>\n<p>\n    \u79c1\u305f\u3061\u304c\u3064\u307e\u305a\u304d\u3084\u3059\u3044\u306e\u304c\u3001<strong>WHERE\u3068HAVING\u306e\u4f7f\u3044\u5206\u3051<\/strong>\u3002<br \/>\n    \u3053\u306e\u9055\u3044\u3001\u3069\u3046\u8aac\u660e\u3067\u304d\u307e\u3059\u304b\uff1f<br \/>\n    \u30dd\u30a4\u30f3\u30c8\u306f\u300cSQL\u304c\u5185\u90e8\u3067\u3069\u3093\u306a\u9806\u756a\u3067\u51e6\u7406\u3055\u308c\u308b\u304b\u300d\u3067\u3059\u3002<br \/>\n    \u5b9f\u306f\u3001<strong>\u66f8\u3044\u305f\u901a\u308a\u306e\u9806\u3067\u306f\u306a\u304f\u3001<\/strong>\u4e0b\u8a18\u306e\u201c7\u30b9\u30c6\u30c3\u30d7\u201d\u3067\u5b9f\u884c\u3055\u308c\u3066\u3044\u307e\u3059\u3002\n  <\/p>\n<h3>\u3010\u56f3\u89e3\u3011SQL\u30af\u30a8\u30ea\u306e7\u6bb5\u968e\u51e6\u7406\u30d5\u30ed\u30fc<\/h3>\n<ul>\n<li><strong>FROM<\/strong> \u2026 \u3069\u306e\u30c6\u30fc\u30d6\u30eb\u304b<\/li>\n<li><strong>JOIN<\/strong> \u2026 \u5fc5\u8981\u306a\u3089\u7d50\u5408<\/li>\n<li><strong>WHERE<\/strong> \u2026 <strong>\u30b0\u30eb\u30fc\u30d7\u5316\u524d<\/strong>\u306b\u5143\u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u3080<\/li>\n<li><strong>GROUP BY<\/strong> \u2026 \u6307\u5b9a\u5217\u3067\u30b0\u30eb\u30fc\u30d7\u5316<\/li>\n<li><strong>HAVING<\/strong> \u2026 <strong>\u30b0\u30eb\u30fc\u30d7\u5316\u5f8c<\/strong>\u306e\u96c6\u8a08\u5024\u3067\u7d5e\u308a\u8fbc\u3080<\/li>\n<li><strong>SELECT<\/strong> \u2026 \u4f55\u3092\u51fa\u3059\u304b\uff08\u96c6\u8a08\u3082\u3053\u3053\uff09<\/li>\n<li><strong>ORDER BY<\/strong> \u2026 \u4e26\u3073\u9806\u3092\u6c7a\u3081\u308b<\/li>\n<\/ul>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aFROM<\/strong><br \/>\n      \u3069\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u304b\u3092\u6307\u5b9a\u3059\u308bSQL\u53e5\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aJOIN<\/strong><br \/>\n      \u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u3001\u95a2\u9023\u3059\u308b\u30c7\u30fc\u30bf\u3092\u307e\u3068\u3081\u3066\u53d6\u5f97\u3059\u308b\u305f\u3081\u306eSQL\u53e5\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aSELECT<\/strong><br \/>\n      \u3069\u306e\u5217\uff08\u9805\u76ee\uff09\u3092\u51fa\u529b\u3059\u308b\u304b\u3092\u6307\u5b9a\u3059\u308bSQL\u53e5\u3002\u96c6\u8a08\u95a2\u6570\u3082\u3053\u3053\u3067\u4f7f\u3046\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aORDER BY<\/strong><br \/>\n      \u7d50\u679c\u306e\u4e26\u3073\u9806\u3092\u6307\u5b9a\u3059\u308bSQL\u53e5\u3002\u6607\u9806\u30fb\u964d\u9806\u306a\u3069\u3092\u6307\u5b9a\u3067\u304d\u308b\u3002\n    <\/p>\n<\/blockquote>\n<p>\n    \uff08<strong>\u51e6\u7406\u306e\u6d41\u308c\u30a4\u30e1\u30fc\u30b8\u56f3\u3092\u3053\u3053\u306b<\/strong>\uff09<br \/>\n    <strong>\u201c\u3069\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u30c7\u30fc\u30bf\u3092\u7d5e\u308b\u304b\u201d\u304c\u3001WHERE\u3068HAVING\u306e\u6c7a\u5b9a\u7684\u306a\u5dee\u3067\u3059\u3002<\/strong>\n  <\/p>\n<ul>\n<li><strong>WHERE<\/strong>\uff1a<strong>\u96c6\u8a08\u30fb\u30b0\u30eb\u30fc\u30d7\u5316\u524d<\/strong>\u3002 <strong>SUM()<\/strong>\u3084<strong>COUNT()<\/strong>\u306a\u3069<strong>\u96c6\u8a08\u95a2\u6570\u306f\u4f7f\u3048\u306a\u3044<\/strong>\u3002<\/li>\n<li><strong>HAVING<\/strong>\uff1a<strong>\u30b0\u30eb\u30fc\u30d7\u5316\u5f8c<\/strong>\u3002 <strong>SUM()<\/strong>\u306a\u3069<strong>\u96c6\u8a08\u6e08\u307f\u306e\u5024\u3067\u7d5e\u308a\u8fbc\u3080<\/strong>\u3002<\/li>\n<\/ul>\n<p>\n    \uff08WHERE\u30fbHAVING\u30fbJOIN\u306e\u9055\u3044\u3068\u5b9f\u8df5\u7684\u306a\u4f7f\u3044\u5206\u3051\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1779\/\" target=\"_blank\" rel=\"noopener\">SQL IN\u30fbEXISTS\u30fbJOIN\u306e\u9055\u3044\u5fb9\u5e95\u89e3\u8aac\uff5c\u5b89\u5168\u3067\u901f\u3044\u9078\u3073\u65b9\u3068\u5b9f\u8df57\u30c1\u30a7\u30c3\u30af<\/a\n    >\u300f\u3082\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09\n  <\/p>\n<blockquote>\n<p>\n      <strong\n        >\u6599\u7406\u3067\u4f8b\u3048\u308b\u3068\u2026<br \/>\n        WHERE\u306f\u300c\u6750\u6599\u306e\u4e0b\u3054\u3057\u3089\u3048\u300d\u3002<br \/>\n        HAVING\u306f\u300c\u5b8c\u6210\u3057\u305f\u6599\u7406\u306e\u5408\u683c\u30c1\u30a7\u30c3\u30af\u300d\u3067\u3059\u3002<\/strong\n      >\n    <\/p>\n<\/blockquote>\n<hr \/>\n<h2>2. \u3053\u308c\u3067\u8179\u843d\u3061\uff01\u73fe\u5834\u3067\u3088\u304f\u4f7f\u3046GROUP BY\u30d1\u30bf\u30fc\u30f33\u9078<\/h2>\n<p>\u5b9f\u969b\u306b\u3001\u58f2\u4e0a\u30c7\u30fc\u30bf\u3067\u300cGROUP BY\u306e\u57fa\u672c\u2192\u5fdc\u7528\u2192HAVING\u7d5e\u308a\u8fbc\u307f\u300d\u307e\u3067\u6d41\u308c\u3092\u8ffd\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>2-1. \u57fa\u672c\uff1a\u300c\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306b\u4ef6\u6570\u300d\u3092\u51fa\u3059\uff08COUNT\uff09<\/h3>\n<pre><code>\r\nSELECT\r\n    product_category,\r\n    COUNT(order_id) AS order_count\r\nFROM\r\n    sales\r\nGROUP BY\r\n    product_category;\r\n<\/code><\/pre>\n<p><strong>\u5b9f\u884c\u4f8b\uff1a<\/strong><\/p>\n<pre><code>\r\n| product_category | order_count |\r\n|------------------|------------|\r\n| \u66f8\u7c4d             | 2          |\r\n| \u5bb6\u96fb             | 2          |\r\n| \u98df\u54c1             | 1          |\r\n<\/code><\/pre>\n<ul>\n<li><strong>GROUP BY<\/strong>\u3067\u300c\u66f8\u7c4d\u30fb\u5bb6\u96fb\u30fb\u98df\u54c1\u300d\u3054\u3068\u306b\u30b0\u30eb\u30fc\u30d7<\/li>\n<li><strong>COUNT<\/strong>\u3067\u5404\u30b0\u30eb\u30fc\u30d7\u306e\u4ef6\u6570\u3092\u53d6\u5f97<\/li>\n<\/ul>\n<hr \/>\n<h3>2-2. \u5fdc\u7528\uff1a\u300c\u30ab\u30c6\u30b4\u30ea\u30fb\u6708\u3054\u3068\u306e\u58f2\u4e0a\u5408\u8a08\u300d\u3092\u51fa\u3059\uff08\u8907\u6570\u5217GROUP BY, SUM\uff09<\/h3>\n<pre><code>\r\nSELECT\r\n    product_category,\r\n    STRFTIME('%Y-%m', order_date) AS sales_month, -- SQLite\u306e\u5834\u5408\r\n    SUM(total_price) AS monthly_sales\r\nFROM\r\n    sales\r\nGROUP BY\r\n    product_category, sales_month;\r\n<\/code><\/pre>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1a\u8907\u6570\u5217GROUP BY<\/strong><br \/>\n      GROUP BY\u30672\u3064\u4ee5\u4e0a\u306e\u5217\u3092\u6307\u5b9a\u3059\u308b\u3068\u3001\u300c\u30ab\u30c6\u30b4\u30ea\u00d7\u6708\u300d\u306a\u3069\u8907\u6570\u8ef8\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3067\u304d\u308b\u3002\n    <\/p>\n<\/blockquote>\n<p><strong>\u5b9f\u884c\u4f8b\uff1a<\/strong><\/p>\n<pre><code>\r\n| product_category | sales_month | monthly_sales |\r\n|------------------|-------------|--------------|\r\n| \u66f8\u7c4d             | 2023-04     | 3500         |\r\n| \u5bb6\u96fb             | 2023-04     | 50000        |\r\n| \u5bb6\u96fb             | 2023-05     | 30000        |\r\n| \u98df\u54c1             | 2023-05     | 500          |\r\n<\/code><\/pre>\n<blockquote>\n<p><strong>\u8907\u6570\u5217\u3067\u30b0\u30eb\u30fc\u30d7\u5316<\/strong>\u3059\u308c\u3070\u3001\u300c\u30ab\u30c6\u30b4\u30ea\u00d7\u6708\u300d\u5358\u4f4d\u306e\u96c6\u8a08\u3082\u7c21\u5358\u3067\u3059\u3002<\/p>\n<\/blockquote>\n<hr \/>\n<h3>2-3. \u5b9f\u8df5\uff1a\u58f2\u4e0a\u304c10\u4e07\u5186\u4ee5\u4e0a\u306e\u30ab\u30c6\u30b4\u30ea\u3060\u3051\u62bd\u51fa\uff08HAVING\uff09<\/h3>\n<pre><code>\r\nSELECT\r\n    product_category,\r\n    SUM(total_price) AS total_sales\r\nFROM\r\n    sales\r\nGROUP BY\r\n    product_category\r\nHAVING\r\n    SUM(total_price) >= 100000;\r\n<\/code><\/pre>\n<ul>\n<li><strong>GROUP BY\u5f8c\u306e\u96c6\u8a08\u5024<\/strong>\uff08\u3053\u3053\u3067\u306fSUM\uff09\u3067\u7d5e\u308b\u306a\u3089\u3001<strong>HAVING<\/strong>\u304c\u5fc5\u9808<\/li>\n<li><strong>WHERE\u3067SUM\u3092\u66f8\u304f\u3068\u30a8\u30e9\u30fc<\/strong>\u306b\u306a\u308b\u7406\u7531\u306f\u3001\u201c\u96c6\u8a08\u524d\u201d\u3060\u304b\u3089<\/li>\n<\/ul>\n<hr \/>\n<h2>3. GROUP BY\u3067\u3088\u304f\u51fa\u308b\u30a8\u30e9\u30fc\u2500\u2500\u539f\u56e0\u3068\u89e3\u6c7a\u7b56<\/h2>\n<p>\n    \u300cSELECT\u306b\u66f8\u3044\u305f\u5217\u304cGROUP BY\u306b\u306a\u3044\u300d<br \/>\n    \u300cWHERE\u3067SUM()\u3057\u305f\u3089\u30a8\u30e9\u30fc\u300d<br \/>\n    \u2026\u79c1\u305f\u3061\u304c\u73fe\u5834\u3067\u201c\u7126\u308b\u201d\u30dd\u30a4\u30f3\u30c8\u3092\u6574\u7406\u3057\u307e\u3059\u3002\n  <\/p>\n<h3>3-1. SELECT\u30ea\u30b9\u30c8\u306e\u5217\u304cGROUP BY\u306b\u7121\u3044<\/h3>\n<pre><code>\r\n-- &#x274c; \u30a8\u30e9\u30fc\u4f8b\r\nSELECT product_category, product_name\r\nFROM sales\r\nGROUP BY product_category;\r\n<\/code><\/pre>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aSELECT\u30ea\u30b9\u30c8<\/strong><br \/>\n      SELECT\u53e5\u3067\u6307\u5b9a\u3057\u305f\u51fa\u529b\u5bfe\u8c61\u306e\u5217\u306e\u4e00\u89a7\u3002GROUP BY\u306b\u542b\u307e\u308c\u306a\u3044\u5217\u306f\u96c6\u7d04\u95a2\u6570\u3067\u307e\u3068\u3081\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\n    <\/p>\n<\/blockquote>\n<p>\n    <strong>\u539f\u56e0\uff1a<\/strong><br \/>\n    \u300c\u66f8\u7c4d\u300d\u30b0\u30eb\u30fc\u30d7\u306e\u4e2d\u306b\u8907\u6570\u306e<strong>product_name<\/strong>\u304c\u3042\u308a\u3001<strong>\u3069\u308c\u3092\u51fa\u3059\u304b\u6c7a\u3081\u3089\u308c\u306a\u3044<\/strong>\u304b\u3089\u3002\n  <\/p>\n<p><strong>\u89e3\u6c7a\uff1a<\/strong><\/p>\n<pre><code>\r\n-- &#x2705; product_name\u3082\u30b0\u30eb\u30fc\u30d7\u5316\r\nSELECT product_category, product_name\r\nFROM sales\r\nGROUP BY product_category, product_name;\r\n<\/code><\/pre>\n<hr \/>\n<h3>3-2. WHERE\u53e5\u3067\u96c6\u8a08\u95a2\u6570\u3092\u4f7f\u3063\u3066\u3044\u308b<\/h3>\n<pre><code>\r\n-- &#x274c; \u30a8\u30e9\u30fc\u4f8b\r\nSELECT product_category, SUM(total_price)\r\nFROM sales\r\nWHERE SUM(total_price) > 10000\r\nGROUP BY product_category;\r\n<\/code><\/pre>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aWHERE\u53e5\u3068\u96c6\u8a08\u95a2\u6570\u306e\u95a2\u4fc2<\/strong><br \/>\n      WHERE\u53e5\u306f\u30b0\u30eb\u30fc\u30d7\u5316\u524d\u306e\u30c7\u30fc\u30bf\u306b\u3057\u304b\u4f7f\u3048\u305a\u3001SUM\u3084COUNT\u306a\u3069\u306e\u96c6\u8a08\u95a2\u6570\u306fHAVING\u53e5\u3067\u4f7f\u3046\u3002\n    <\/p>\n<\/blockquote>\n<p>\n    <strong>\u539f\u56e0\uff1a<\/strong><br \/>\n    <strong>WHERE\u53e5\u306f\u201c\u30b0\u30eb\u30fc\u30d7\u5316\u524d\u201d\u306e\u30c7\u30fc\u30bf\u306b\u3057\u304b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u305a\u3001SUM\u5024\u306f\u307e\u3060\u7121\u3044<\/strong>\u304b\u3089\u3002\n  <\/p>\n<p><strong>\u89e3\u6c7a\uff1a<\/strong><\/p>\n<pre><code>\r\n-- &#x2705; HAVING\u3067\u7d5e\u308b\r\nSELECT product_category, SUM(total_price) AS total_sales\r\nFROM sales\r\nGROUP BY product_category\r\nHAVING SUM(total_price) > 10000;\r\n<\/code><\/pre>\n<hr \/>\n<h2>4. \u201cGROUP BY\u8ff7\u5b50\u201d\u3092\u8131\u5374\u3059\u308b\u305f\u3081\u306e3\u30dd\u30a4\u30f3\u30c8\u307e\u3068\u3081<\/h2>\n<ul>\n<li><strong>WHERE\u306f\u7d20\u6750\u9078\u3073<\/strong>\uff1a\u30b0\u30eb\u30fc\u30d7\u5316<strong>\u524d<\/strong>\u306e\u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u3080<\/li>\n<li><strong>GROUP BY\u3067\u675f\u306d\u308b<\/strong>\uff1a\u6307\u5b9a\u3057\u305f\u5217\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b<\/li>\n<li><strong>HAVING\u306f\u5b8c\u6210\u54c1\u9078\u3073<\/strong>\uff1a\u30b0\u30eb\u30fc\u30d7\u5316<strong>\u5f8c<\/strong>\u306e\u96c6\u8a08\u5024\u3067\u7d5e\u308b<\/li>\n<\/ul>\n<p>\u3053\u308c\u3092\u62bc\u3055\u3048\u308c\u3070\u3001<strong>SELECT\u306b\u4f55\u3092\u66f8\u304f\uff1fWHERE\uff1fHAVING\uff1f<\/strong>\u2026\u3067\u8ff7\u3046\u3053\u3068\u306f\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30da\u3057\u3066\u3001\u307e\u305a\u306f\u52d5\u304b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<hr \/>\n<h2>FAQ<\/h2>\n<ul>\n<li><strong>Q. NULL\u3092\u542b\u3080\u5217\u3092GROUP BY\u3059\u308b\u3068\uff1f<\/strong><br \/>\u2192 <strong>NULL\u30821\u3064\u306e\u72ec\u7acb\u3057\u305f\u30b0\u30eb\u30fc\u30d7<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n<li>\n      <strong>Q. GROUP BY\u3068\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570(PARTITION BY)\u306e\u9055\u3044\u306f\uff1f<\/strong><br \/>\u2192<br \/>\n      <strong>GROUP BY\u306f\u96c6\u7d04\u3057\u3066\u884c\u6570\u304c\u6e1b\u308b<\/strong>\u3001<strong>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306f\u5143\u306e\u884c\u6570\u305d\u306e\u307e\u307e<\/strong>\u3067\u96c6\u8a08\u5024\u304c\u4ed8\u4e0e\u3055\u308c\u307e\u3059\u3002\n    <\/li>\n<li><strong>Q. HAVING\u53e5\u3067\u8907\u6570\u6761\u4ef6\u306f\uff1f<\/strong><br \/>\u2192 <strong>AND\/OR<\/strong>\u3067\u8907\u6570\u6761\u4ef6\u53ef<br \/>\u4f8b\uff1a<strong>HAVING SUM(total_price) > 10000 AND COUNT(order_id) >= 2<\/strong><\/li>\n<li><strong>Q. GROUP BY\u3068DISTINCT\u306e\u4f7f\u3044\u5206\u3051\u306f\uff1f<\/strong><br \/>\u2192 <strong>DISTINCT<\/strong>\u306f\u91cd\u8907\u9664\u53bb\u3001<strong>GROUP BY<\/strong>\u306f\u96c6\u8a08\u3084\u30b0\u30eb\u30fc\u30d7\u5358\u4f4d\u3067\u8a08\u7b97\u3057\u305f\u3044\u3068\u304d\u3002<\/li>\n<li><strong>Q. \u96c6\u8a08\u5024\u3067\u4e26\u3073\u66ff\u3048\u305f\u3044<\/strong><br \/>\u2192 <strong>ORDER BY SUM(total_price) DESC<\/strong> \u306a\u3069\u3067OK\u3002<\/li>\n<li><strong>Q. \u306a\u305cGROUP BY\u306e\u5f8c\u306bWHERE\u306f\u4f7f\u3048\u306a\u3044\uff1f<\/strong><br \/>\u2192 <strong>SQL\u306fWHERE\u2192GROUP BY\u306e\u9806\u3067\u51e6\u7406<\/strong>\u3002<br \/>\u3064\u307e\u308a\u3001<strong>\u201c\u672a\u6765\u306e\u5024\u201d\u3067\u5143\u30c7\u30fc\u30bf\u3092\u7d5e\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093<\/strong>\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aNULL<\/strong><br \/>\n      \u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3092\u793a\u3059\u7279\u5225\u306a\u5024\u3002GROUP BY\u3067\u306fNULL\u30821\u3064\u306e\u30b0\u30eb\u30fc\u30d7\u3068\u3057\u3066\u6271\u308f\u308c\u308b\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1a\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\uff08PARTITION BY\uff09<\/strong><br \/>\n      \u5143\u306e\u884c\u6570\u3092\u4fdd\u3063\u305f\u307e\u307e\u3001\u30b0\u30eb\u30fc\u30d7\u3054\u3068\u306e\u96c6\u8a08\u5024\u3092\u5404\u884c\u306b\u4ed8\u4e0e\u3067\u304d\u308bSQL\u306e\u6a5f\u80fd\u3002\n    <\/p>\n<p>\n      <strong>\u7528\u8a9e\u89e3\u8aac\uff1aDISTINCT<\/strong><br \/>\n      \u91cd\u8907\u3059\u308b\u30c7\u30fc\u30bf\u3092\u9664\u5916\u3057\u3001\u4e00\u610f\u306a\u5024\u3060\u3051\u3092\u62bd\u51fa\u3059\u308bSQL\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u3002\n    <\/p>\n<p>\n      \uff08DISTINCT\u30fbGROUP BY\u30fbROW_NUMBER\u306e\u9055\u3044\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1713\/\" target=\"_blank\" rel=\"noopener\">SQL\u91cd\u8907\u9664\u5916\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u5fb9\u5e95\u6bd4\u8f03\uff5cDISTINCT\u30fbGROUP BY\u30fbROW_NUMBER\u306e\u9055\u3044\u306f\uff1f<\/a\n      >\u300f\u3082\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09\n    <\/p>\n<\/blockquote>\n<hr \/>\n<p>\n    <strong>\u300cSQL\u306e\u5b9f\u884c\u9806\u5e8f\u300d\u3092\u77e5\u308c\u3070\u3001\u3082\u3046GROUP BY\u306f\u6016\u304f\u306a\u3044\u2500\u2500\u660e\u65e5\u304b\u3089\u96c6\u8a08\u30af\u30a8\u30ea\u3092\u201c\u81ea\u4fe1\u3092\u6301\u3063\u3066\u201d\u66f8\u3051\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/strong>\n  <\/p>\n<hr \/>\n<\/div>\n","protected":false},"featured_media":1871,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/1870"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/1871"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}