{"id":5872,"date":"2025-11-06T20:11:12","date_gmt":"2025-11-06T11:11:12","guid":{"rendered":"https:\/\/www.bold.ne.jp\/engineer-club\/?p=5872"},"modified":"2025-10-08T14:12:37","modified_gmt":"2025-10-08T05:12:37","slug":"sql_sumfunction","status":"publish","type":"post","link":"https:\/\/www.bold.ne.jp\/engineer-club\/sql_sumfunction","title":{"rendered":"\u3010SQL\u3011SUM\u95a2\u6570\u3068\u306f\uff1f \u6570\u5024\u3092\u5408\u8a08\u3059\u308b\u65b9\u6cd5\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac"},"content":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u3067\u306fSQL\u3067\u306eSUM\u95a2\u6570\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u8d85\u521d\u5fc3\u8005\u5411\u3051\u306b\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">\u76ee\u6b21<\/p><ul class=\"toc_list\"><li><a href=\"#1_SUM\">1\uff0e SUM\u95a2\u6570\u3068\u306f<\/a><ul><li><a href=\"#-_SUM\">\uff11-\uff11\uff0e SUM\u95a2\u6570\u3068\u306f<\/a><\/li><li><a href=\"#-SUM\">\uff11-\uff12\uff0eSUM\u95a2\u6570\u306e\u69cb\u6587<\/a><\/li><\/ul><\/li><li><a href=\"#2_SUM\">2\uff0e SUM\u95a2\u6570\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/a><ul><li><a href=\"#i\">\uff12-\uff11\uff0e\u6570\u5024\u306e\u5408\u8a08<\/a><\/li><li><a href=\"#-NULL\">\uff12-\uff12\uff0eNULL\u5024\u306e\u6271\u3044<\/a><\/li><\/ul><\/li><li><a href=\"#3WHEREGROUP_BY\">3\uff0eWHERE\u53e5\u3084GROUP BY\u3068\u306e\u7d44\u307f\u5408\u308f\u305b<\/a><ul><li><a href=\"#-WHERE\">\uff13-\uff11\uff0eWHERE\u53e5\u3067\u5408\u8a08\u306e\u5bfe\u8c61\u3092\u7d5e\u308b<\/a><\/li><li><a href=\"#-GROUP_BY\">\uff13-\uff12\uff0eGROUP BY\u53e5\u3068\u7d44\u307f\u5408\u308f\u305b\u308b<\/a><\/li><\/ul><\/li><li><a href=\"#i-2\">\uff14\uff0e\u307e\u3068\u3081<\/a><\/li><\/ul><\/div>\n<h2><span id=\"1_SUM\">1\uff0e SUM\u95a2\u6570\u3068\u306f<\/span><\/h2>\n<p>\u300cSUM\u300d\u306b\u3064\u3044\u3066\u306fEXCEL\u3067\u3082\u3088\u304f\u4f7f\u3046\u95a2\u6570\u306e\u305f\u3081SQL\u304c\u521d\u3081\u3066\u3067\u3082\u99b4\u67d3\u307f\u6df1\u3044\u65b9\u3082\u591a\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\n\u672c\u8a18\u4e8b\u3067\u306fSQL\u3067\u306e\u300cSUM\u300d\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.bold.ne.jp\/engineer-club\/wp-content\/uploads\/2025\/10\/\u3042.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.bold.ne.jp\/engineer-club\/wp-content\/uploads\/2025\/10\/\u3042.jpg\" alt=\"\u30aa\u30fc\u30c8\u30b5\u30e0\" width=\"310\" height=\"310\" class=\"alignnone  wp-image-5873\" srcset=\"https:\/\/www.bold.ne.jp\/engineer-club\/wp-content\/uploads\/2025\/10\/\u3042.jpg 1024w, https:\/\/www.bold.ne.jp\/engineer-club\/wp-content\/uploads\/2025\/10\/\u3042-768x768.jpg 768w\" sizes=\"(max-width: 310px) 100vw, 310px\" \/><\/a><\/p>\n<h3><span id=\"-_SUM\">\uff11-\uff11\uff0e SUM\u95a2\u6570\u3068\u306f<\/span><\/h3>\n<p>SUM \u95a2\u6570\u306f\u6307\u5b9a\u3057\u305f\u6570\u5024\u30ab\u30e9\u30e0\uff08\u5217\uff09\u306e\u5408\u8a08\u3092\u6c42\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u58f2\u4e0a\u306e\u5408\u8a08\u3084\u3001\u6570\u91cf\u306e\u5408\u8a08\u306a\u3069\u306e\u8a08\u7b97\u306b\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n<h3><span id=\"-SUM\">\uff11-\uff12\uff0eSUM\u95a2\u6570\u306e\u69cb\u6587<\/span><\/h3>\n<p>\u69cb\u6587\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<div class=\"attention a-1\">\n<p><strong>SUM\u306e\u69cb\u6587<\/p>\n<p><\/strong><\/p>\n<p>SELECT SUM(\u5217\u540d) FROM \u30c6\u30fc\u30d6\u30eb\u540d;<\/p>\n<\/div>\n<h2><span id=\"2_SUM\">2\uff0e SUM\u95a2\u6570\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/span><\/h2>\n<p>\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u3092\u898b\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h3><span id=\"i\">\uff12-\uff11\uff0e\u6570\u5024\u306e\u5408\u8a08<\/span><\/h3>\n<p>\u6b21\u306esales\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u3063\u3066\u3001\u5217 price \u306e\u30c7\u30fc\u30bf\u3092\u5408\u8a08\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\u30c6\u30fc\u30d6\u30eb\u540d\uff1asales<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8211;+<\/strong><br \/>\n<strong>| customer_name | product | price | order_date | cancel |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8211;+<\/strong><br \/>\n<strong>| \u682a\u5f0f\u4f1a\u793eA | \u643a\u5e2f\u96fb\u8a71 | <span style=\"color: #99cc00;\">100000<\/span> | <span style=\"color: #99cc00;\">2024-06-01 10:00:00<\/span> | <span style=\"color: #99cc00;\">0<\/span> |<\/strong><br \/>\n<strong>| B\u682a\u5f0f\u4f1a\u793e | \u30d7\u30ea\u30f3\u30bf\u30fc | <span style=\"color: #99cc00;\">20000<\/span> |<span style=\"color: #99cc00;\"> 2024-06-02 11:30:00<\/span> | <span style=\"color: #99cc00;\">0<\/span> |<\/strong><br \/>\n<strong>| C\u682a\u5f0f\u4f1a\u793e | \u30e2\u30cb\u30bf\u30fc | <span style=\"color: #99cc00;\">30000<\/span> | <span style=\"color: #99cc00;\">2024-06-03 14:00:00<\/span> |<span style=\"color: #99cc00;\"> 0<\/span> |<\/strong><br \/>\n<strong>| \u682a\u5f0f\u4f1a\u793eA | \u30e2\u30d0\u30a4\u30eb\u30d0\u30c3\u30c6\u30ea\u30fc |<span style=\"color: #3366ff;\"> NULL<\/span> | <span style=\"color: #99cc00;\">2024-06-03 16:00:00<\/span> | <span style=\"color: #99cc00;\">1<\/span> |<\/strong><br \/>\n<strong>| \u682a\u5f0f\u4f1a\u793eA | \u30d7\u30ed\u30b8\u30a7\u30af\u30bf\u30fc | <span style=\"color: #99cc00;\">350000<\/span> | <span style=\"color: #99cc00;\">2024-06-03 16:00:00<\/span> | <span style=\"color: #99cc00;\">0<\/span> |<\/strong><br \/>\n<strong>| B\u682a\u5f0f\u4f1a\u793e | \u30ce\u30fc\u30c8\u30d1\u30bd\u30b3\u30f3 | <span style=\"color: #99cc00;\">250000<\/span> | <span style=\"color: #99cc00;\">2024-06-04 12:00:00<\/span> | <span style=\"color: #99cc00;\">0<\/span> |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+-<\/strong><\/p>\n<pre class=\"prettyprint lang:default decode:true\">SELECT SUM(price) FROM sales;<\/pre>\n<p><strong>\u3010SELECT\u7d50\u679c\u3011<\/strong><br \/>\n<strong>mysql&gt; <span style=\"color: #3366ff;\">SELECT<\/span> <span style=\"color: #e0e036;\">SUM<\/span>(price) <span style=\"color: #3366ff;\">FROM<\/span> sales;<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>| <span style=\"color: #e0e036;\">SUM<\/span>(price) |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>| <span style=\"color: #99cc00;\">750000<\/span> |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>1 <span style=\"color: #3366ff;\">row in set<\/span> (<span style=\"color: #99cc00;\">0.00<\/span> sec)<\/strong><\/p>\n<h3><span id=\"-NULL\">\uff12-\uff12\uff0eNULL\u5024\u306e\u6271\u3044<\/span><\/h3>\n<p>SUM\u95a2\u6570\u306f\u30c7\u30fc\u30bf\u306bNULL\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5834\u5408\u3001NULL\u3092\u7121\u8996\u3057\u3066\u5408\u8a08\u3092\u6c42\u3081\u307e\u3059\u3002<br \/>\n\u4e0a\u8a18\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3082 price \u306b\u306fNULL\u306e\u30c7\u30fc\u30bf\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u554f\u984c\u306a\u304f\u8a08\u7b97\u304c\u3067\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2><span id=\"3WHEREGROUP_BY\">3\uff0eWHERE\u53e5\u3084GROUP BY\u3068\u306e\u7d44\u307f\u5408\u308f\u305b<\/span><\/h2>\n<p>\u3067\u306f\u6b21\u306bWHERE\u53e5\u3084GROUP BY \u53e5\u3092\u4f7f\u3063\u3066\u6761\u4ef6\u3092\u7d5e\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<h3><span id=\"-WHERE\">\uff13-\uff11\uff0eWHERE\u53e5\u3067\u5408\u8a08\u306e\u5bfe\u8c61\u3092\u7d5e\u308b<\/span><\/h3>\n<p>WHERE\u53e5\u3092\u52a0\u3048\u3001\u682a\u5f0f\u4f1a\u793eA \u3060\u3051\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002<br \/>\n<span class=\"f-marker-w\">\u682a\u5f0f\u4f1a\u793eA\u3060\u3051\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3059\u308b\u3002<br \/>\n<\/span><\/p>\n<pre class=\"prettyprint lang:default decode:true\">SELECT SUM(price) FROM sales WHERE customer_name = '\u682a\u5f0f\u4f1a\u793eA';<\/pre>\n<p><strong>\u3010SELECT\u7d50\u679c\u3011<\/strong><br \/>\n<strong>mysql&gt;<span style=\"color: #3366ff;\"> SELECT<\/span> <span style=\"color: #e0e036;\">SUM<\/span>(price) <span style=\"color: #3366ff;\">FROM<\/span> sales <span style=\"color: #3366ff;\">WHERE<\/span> customer_name = <span style=\"color: #ff9900;\">&#8216;\u682a\u5f0f\u4f1a\u793eA&#8217;;<\/span><\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>| <span style=\"color: #e0e036;\">SUM<\/span>(price) |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>| <span style=\"color: #99cc00;\">450000<\/span> |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>1<span style=\"color: #3366ff;\"> row in set<\/span> (<span style=\"color: #99cc00;\">0.00<\/span> sec)<\/strong><\/p>\n<p>\u682a\u5f0f\u4f1a\u793eA\u3060\u3051\u306e\u5408\u8a08\u3092\u6c42\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<h3><span id=\"-GROUP_BY\">\uff13-\uff12\uff0eGROUP BY\u53e5\u3068\u7d44\u307f\u5408\u308f\u305b\u308b<\/span><\/h3>\n<p>\u3067\u306f\u6b21\u306bGROUP BY\u53e5\u3092\u4f7f\u3063\u3066\u9867\u5ba2(customer_name)\u3054\u3068\u306e\u5408\u8a08\u3092\u6c42\u3081\u3066\u307f\u307e\u3059\u3002<br \/>\n<span class=\"f-marker-w\">\u9867\u5ba2(customer_name)\u3054\u3068\u306e\u5408\u8a08\u3092\u6c42\u3081\u308b\u3002<\/span><\/p>\n<pre class=\"prettyprint lang:default decode:true\">SELECT customer_name, SUM(price) FROM sales GROUP BY customer_name;<\/pre>\n<p><strong>\u3010SELECT\u7d50\u679c\u3011<\/strong><br \/>\n<strong>mysql&gt; <span style=\"color: #3366ff;\">SELECT<\/span> customer_name, SUM(price) <span style=\"color: #3366ff;\">FROM<\/span> sales <span style=\"color: #3366ff;\">GROUP BY<\/span> customer_name;<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>| customer_name | <span style=\"color: #e0e036;\">SUM<\/span>(price) |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>| \u682a\u5f0f\u4f1a\u793eA | <span style=\"color: #99cc00;\">450000<\/span> |<\/strong><br \/>\n<strong>| B\u682a\u5f0f\u4f1a\u793e | <span style=\"color: #99cc00;\">270000<\/span> |<\/strong><br \/>\n<strong>| C\u682a\u5f0f\u4f1a\u793e | <span style=\"color: #99cc00;\">30000<\/span> |<\/strong><br \/>\n<strong>+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<\/strong><br \/>\n<strong>3<span style=\"color: #3366ff;\"> rows in set<\/span> (<span style=\"color: #99cc00;\">0.00<\/span> sec)<\/strong><\/p>\n<p>\u9867\u5ba2\u3054\u3068\u306e\u5408\u8a08\u3092\u6c42\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<h2><span id=\"i-2\">\uff14\uff0e\u307e\u3068\u3081<\/span><\/h2>\n<div class=\"attention a-1\">\n<p>\u30fbSUM\u95a2\u6570\u306f\u6570\u5024\u306e\u5408\u8a08\u3092\u6c42\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u308b<br \/>\n\u30fbNULL\u306f\u7121\u8996\u3057\u3066\u8a08\u7b97\u3055\u308c\u308b<br \/>\n\u30fbWHERE\u53e5\u3084GROUP BY\u53e5\u3068\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u69d8\u3005\u306a\u96c6\u8a08\u304c\u53ef\u80fd<\/p>\n<\/div>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<br \/>\n\u6700\u5f8c\u307e\u3067\u304a\u8aad\u307f\u3044\u305f\u3060\u304d\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3010\u52d5\u4f5c\u691c\u8a3cDB\u3011<br \/>\nmySQL 8.0.39<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u3067\u306fSQL\u3067\u306eSUM\u95a2\u6570\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u8d85\u521d\u5fc3\u8005\u5411\u3051\u306b\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002 \u76ee\u6b211\uff0e SUM\u95a2\u6570\u3068\u306f\uff11-\uff11\uff0e SUM\u95a2\u6570\u3068\u306f\uff11-\uff12\uff0eSUM\u95a2\u6570\u306e\u69cb\u65872\uff0e SUM\u95a2\u6570\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\uff12-\uff11\uff0e\u6570\u5024\u306e\u5408\u8a08\uff12-\uff12\uff0eN [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":1181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/5872"}],"collection":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/comments?post=5872"}],"version-history":[{"count":8,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/5872\/revisions"}],"predecessor-version":[{"id":5882,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/posts\/5872\/revisions\/5882"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/media\/1181"}],"wp:attachment":[{"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/media?parent=5872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bold.ne.jp\/engineer-club\/wp-json\/wp\/v2\/categories?post=5872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}