{"id":4285,"date":"2019-01-30T08:09:53","date_gmt":"2019-01-30T00:09:53","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4285"},"modified":"2019-01-30T08:09:53","modified_gmt":"2019-01-30T00:09:53","slug":"hive-sql%e5%ad%a6%e4%b9%a0%e6%95%b4%e7%90%86_4","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4285.html","title":{"rendered":"Hive SQL\u5b66\u4e60\u6574\u7406_4"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u7ee7\u7eed\u6574\u7406\u6700\u8fd1\u5b66\u5230\u6216\u662f\u7528\u5230\u7684Hive SQL\u77e5\u8bc6\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><span style=\"color: #ff0000;\">0\u3001Hive SQL\u4e2d\u7684 having \u5b50\u53e5\u4ee5\u53ca\u5b83\u4e0e where \u5b50\u53e5\u7684\u533a\u522b<\/span><\/h6>\n<pre class=\"lang:default decode:true \">-- Hive\u57280.7.0\u7248\u672c\u4e2d\u589e\u52a0\u4e86\u5bf9HAVING\u5b50\u53e5\u7684\u652f\u6301\u3002\u5728Hive\u7684\u8001\u7248\u672c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u5b50\u67e5\u8be2\u53ef\u4ee5\u8fbe\u5230\u540c\u6837\u7684\u6548\u679c\uff0c\u4f8b\u5982\uff1a\r\nSELECT col1 FROM t1 GROUP BY col1 HAVING SUM(col2) &gt; 10\r\n-- \u8001\u7248\u672c\u7684\u5b9e\u73b0\r\nSELECT col1 FROM (SELECT col1, SUM(col2) AS col2sum FROM t1 GROUP BY col1) t2 WHERE t2.col2sum &gt; 10\r\n<\/pre>\n<p>&amp;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>having\u5b50\u53e5\u4e0ewhere\u90fd\u662f\u8fc7\u6ee4\u8bed\u53e5\u3002<\/strong><\/span><\/p>\n<ul>\n<li><strong><span style=\"color: #000000;\"><span style=\"color: #ff0000;\">where \u5b50\u53e5<\/span>\u7684\u4f5c\u7528\u662f\u5728\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\u524d\uff0c\u5c06\u4e0d\u7b26\u5408where\u6761\u4ef6\u7684\u884c\u53bb\u6389\uff0c\u5373<span style=\"color: #ff0000;\">\u5728\u5206\u7ec4\u4e4b\u524d\u8fc7\u6ee4\u6570\u636e\uff0c\u6761\u4ef6\u4e2d\u4e0d\u80fd\u5305\u542b\u805a\u5408\u51fd\u6570<\/span><\/span><\/strong>\uff0c\u4f7f\u7528where\u6761\u4ef6\u663e\u793a\u7279\u5b9a\u7684\u884c\u3002<\/li>\n<li><strong><span style=\"color: #ff0000;\">having \u5b50\u53e5<\/span>\u7684\u4f5c\u7528\u662f\u7b5b\u9009\u6ee1\u8db3\u6761\u4ef6\u7684\u7ec4\uff0c\u5373<span style=\"color: #ff0000;\">\u5728\u5206\u7ec4\u4e4b\u540e\u8fc7\u6ee4\u6570\u636e<\/span>\uff0c\u6761\u4ef6\u4e2d\u7ecf\u5e38\u5305\u542b\u805a\u5408\u51fd\u6570<\/strong>\uff0c\u4f7f\u7528having\u6761\u4ef6\u663e\u793a\u7279\u5b9a\u7684\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5206\u7ec4\u6807\u51c6\u8fdb\u884c\u5206\u7ec4\u3002<strong>\u548cgroup by\u4e00\u6837\uff0chaving \u5b50\u53e5\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u4e5f\u5fc5\u987b\u51fa\u73b0\u5728select\u5217\u8868\u4e2d\u3002<\/strong><\/li>\n<li>\u603b\u4e4b\uff0cselect \u7528where\u8fc7\u6ee4\uff0c\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7ec4\u3002\u800chaving \u7528\u5728group by\u540e\uff0c\u914d\u5408\u4f7f\u7528\uff0c\u8fc7\u6ee4\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">-- \u5f53\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u540c\u65f6\u51fa\u73b0\u4e86where,group by,having,order by\u7684\u65f6\u5019\uff0c\u6267\u884c\u987a\u5e8f\u548c\u7f16\u5199\u987a\u5e8f\r\nSELECT column_name, aggregate_function(column_name)\r\nFROM table_name\r\nWHERE column_name operator value\r\nGROUP BY column_name\r\nHAVING aggregate_function(column_name) operator value\r\nORDER BY column_name\r\n<\/pre>\n<h6><strong><span style=\"color: #ff0000;\">1\u3001Hive SQL\u4e2d\u7684\u6761\u4ef6\u5224\u65ad\u51fd\u6570<\/span>\uff08\u7c7b\u4f3c\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684if..else\uff09<\/strong><\/h6>\n<pre class=\"lang:default decode:true\">\u2022 If \u51fd\u6570 : if\r\n\u8bed\u6cd5: if(boolean testCondition, T valueTrue, T valueFalseOrNull)\r\n\u8fd4\u56de\u503c: T\r\n\u8bf4\u660e: \u5f53\u6761\u4ef6testCondition\u4e3aTRUE\u65f6\uff0c\u8fd4\u56devalueTrue\uff1b\u5426\u5219\u8fd4\u56devalueFalseOrNull\r\n\r\nhive&gt; select if(1=2,100,200);\r\n200\r\nhive&gt; select if(1=1,100,200);\r\n100\r\n<\/pre>\n<h6><strong>2\u3001<\/strong><strong>Hive SQL\u4e2d\u7684\u975e\u7a7a\u67e5\u627e\u51fd\u6570<\/strong><\/h6>\n<pre class=\"lang:default decode:true\">\u2022 \u975e\u7a7a\u67e5\u627e\u51fd\u6570 : COALESCE\r\n\u8bed\u6cd5: COALESCE(T v1, T v2, \u2026)\r\n\u8fd4\u56de\u503c: T\r\n\u8bf4\u660e:  \u8fd4\u56de\u53c2\u6570\u4e2d\u7684\u7b2c\u4e00\u4e2a\u975e\u7a7a\u503c\uff1b\u5982\u679c\u6240\u6709\u503c\u90fd\u4e3aNULL\uff0c\u90a3\u4e48\u8fd4\u56deNULL\r\n\r\nhive&gt; select COALESCE(null,'100','50');\r\n100\r\n<\/pre>\n<h6><strong>3\u3001Hive SQL\u4e2d\u5d4c\u5957\u7684if..else\u8be5\u5982\u4f55\u8868\u8fbe\uff1f<\/strong><\/h6>\n<pre class=\"lang:default decode:true\">\u2022 \u6761\u4ef6\u5224\u65ad\u51fd\u6570\uff1a CASE\r\n\u8bed\u6cd5 : CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END\r\n\u8fd4\u56de\u503c : T\r\n\u8bf4\u660e\uff1a\u5982\u679c a \u7b49\u4e8e b \uff0c\u90a3\u4e48\u8fd4\u56de c \uff1b\u5982\u679c a \u7b49\u4e8e d \uff0c\u90a3\u4e48\u8fd4\u56de e \uff1b\u5426\u5219\u8fd4\u56de f\r\n\r\nhive&gt; select case 100\r\n        when 50 then 'tom'\r\n        when 100 then 'mary'\r\n        else 'tim'\r\n      end;\r\n-- mary<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true\">if(a, 1, if(b, 2, if(c, 3, 4)))\r\n# \u8fd9\u4e24\u8005\u7b49\u4ef7\r\nif (a) {\r\n  1\r\n} else if (b) {\r\n  2\r\n} else if (c) {\r\n  3\r\n} else {\r\n  4\r\n}\r\n# \u7528 case \u8bed\u53e5\u8fdb\u884c\u8868\u8fbe\r\nCASE\r\n  WHEN (condition1) THEN result1\r\n  WHEN (condition2) THEN result2\r\n  WHEN (condition3) THEN result3 \r\n  WHEN (condition4) THEN result4\r\n  ELSE result_default \r\nEND AS attribute_name<\/pre>\n<h6><span style=\"color: #ff0000;\"><strong>4\u3001Hive SQL\u4e2dunion all\u7684\u4f7f\u7528<\/strong><\/span><\/h6>\n<p><strong>SQL\u4e2d union all \u6709\u4ec0\u7528\uff1f<\/strong><br \/>\n<span style=\"color: #ff0000;\">\u7b80\u800c\u8a00\u4e4b\uff1a\u5c31\u662f\u628a2\u4e2a\u5177\u6709\u76f8\u540c\u5217\u53ca\u6570\u636e\u7c7b\u578b\u7684\u7ed3\u679c\u653e\u5230\u4e00\u8d77\u663e\u793a\uff0c\u5e76\u4e14\u4e0d\u53bb\u91cd\u3002<\/span><\/p>\n<p><strong>\u4ec0\u60c5\u51b5\u4e0b\u7528union\uff1f\u4ec0\u60c5\u51b5\u4e0b\u7528union all\uff1f<\/strong><br \/>\nunion\u4e0eunion all\u7684\u533a\u522b\u662f\uff1a<br \/>\n\u524d\u8005\u4f1a\u628a\u4e24\u4e2a\u8bb0\u5f55\u96c6\u4e2d\u76f8\u540c\u7684\u8bb0\u5f55\u5408\u5e76\uff0c\u800c\u540e\u8005\u4e0d\u4f1a\uff0c\u6027\u80fd\u4e0a\u524d\u8005\u4f18\u3002\u5f53\u524dHive\u53ea\u652f\u6301UNION ALL(bag union)\u3002\u4e0d\u80fd\u6d88\u9664\u91cd\u590d\u884c\uff0c<span style=\"color: #ff0000;\"><strong>\u6bcf\u4e2aselect\u8bed\u53e5\u8fd4\u56de\u7684\u5217\u7684\u6570\u91cf\u548c\u540d\u5b57\u5fc5\u987b\u4e00\u6837\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u8bed\u6cd5\u9519\u8bef\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true \">select 'a','b','c' from (select 0) t\r\nunion all\r\nselect 'ca','cb','cc' from (select 0) t\r\n\/*\r\na\tb\tc\r\nca\tcb\tcc\r\n*\/\r\n\r\nselect 'a','b','c'\r\nunion all\r\nselect 'ca','cb','cc'\r\n\/*\r\na\tb\tc\r\nca\tcb\tcc\r\n*\/\r\n\r\nselect 'a','b','c'\r\nunion all\r\nselect 'ca','cb'\r\n\/*\r\nsql\u8bed\u6cd5\u89e3\u6790\u9519\u8bef, Union\u8bed\u53e5\u7684\u5217\u6570\u4e0d\u5339\u914d\r\n*\/\r\n<\/pre>\n<h6><strong>5\u3001Hive SQL\u4e2d\u5b57\u7b26\u4e32\u5207\u5206\u51fd\u6570split\u7684\u4f7f\u7528\u793a\u4f8b<\/strong><\/h6>\n<p><span style=\"color: #ff0000;\"><strong>\u5bf9\u4e8e\u62ec\u53f7\u300c)\u300d\u8fd9\u79cd\u7279\u6b8a\u7b26\u53f7\u9700\u8981\u8fdb\u884c\u8f6c\u4e49\uff0c\u800c\u4e14\u9700\u8981\u591a\u4e2a\u8f6c\u4e49\u7b26\uff01<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">select split('python-requests\/2.9.1', '\/')\r\n-- [\"python-requests\",\"2.9.1\"]\r\n\r\nselect split('python-requests\/2.9.1', ')')\r\nselect split('python-requests\/2.9.1', '\\)')\r\n\/*\r\n    FAILED: PatternSyntaxException Unmatched closing ')'\r\n*\/\r\n\r\nselect split('python-requests\/2.9.1', '\\\\)')\r\n-- [\"python-requests\/2.9.1\"]\r\n\r\nselect split('python-requests\/2.9.1', '\\\\)')[0]\r\n-- python-requests\/2.9.1\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+Select#LanguageManualSelect-HAVINGClause\">Hive SQL\u4e2d\u7684 HAVING \u5b50\u53e5<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/longshenlmj\/article\/details\/17757149\">GROUP BY,WHERE,HAVING\u95f4\u7684\u533a\u522b\u548c\u7528\u6cd5<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/Shine_rise\/article\/details\/54934242\">SQL\u4e2dwhere, group by, having\u7684\u7528\u6cd5\u548c\u533a\u522b<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/superhosts\/article\/details\/39298529\">\u5f53\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\u540c\u65f6\u51fa\u73b0\u4e86where,group by,having,order by\u7684\u65f6\u5019\uff0c\u6267\u884c\u987a\u5e8f\u548c\u7f16\u5199\u987a\u5e8f<\/a><\/li>\n<li><a href=\"http:\/\/whlminds.com\/2015\/04\/05\/where-and-having-in-sql\/\">SQL\u4e2dWHERE\u4e0eHAVING\u7528\u6cd5<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/kxdblog\/p\/4034243.html\">hive:\u6761\u4ef6\u5224\u65ad\u51fd\u6570<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/32472801\/whats-the-best-way-to-write-if-else-if-else-if-else-in-hive\">Hive\u4e2d\u5d4c\u5957\u7684if..else\u8be5\u5982\u4f55\u8868\u8fbe\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/buster2014\/article\/details\/50143247\">hive union all \u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+Union\">https:\/\/cwiki.apache.org\/confluence\/display\/Hive\/LanguageManual+Union<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/14096968\/hiveql-union-all\">HiveQL UNION ALL<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/wanghai__\/article\/details\/4712555\">Union\u548cUnion All\u7684\u533a\u522b<\/a><\/li>\n<li><a href=\"https:\/\/www.jianshu.com\/p\/135cc10f2541\">hive UNION\u548c\u5b50\u67e5\u8be2<\/a><\/li>\n<li><a href=\"https:\/\/superlxw1234.iteye.com\/blog\/1536440\">hive\u4e2d\u5408\u7406\u4f7f\u7528union all\u4e0emulti insert<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7ee7\u7eed\u6574\u7406\u6700\u8fd1\u5b66\u5230\u6216\u662f\u7528\u5230\u7684Hive SQL\u77e5\u8bc6\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a 0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[1308,1261,357,1262,1307],"class_list":["post-4285","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-having","tag-hive","tag-split","tag-sql","tag-union-all"],"views":15367,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4285"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4285\/revisions"}],"predecessor-version":[{"id":4287,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4285\/revisions\/4287"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}