{"id":2614,"date":"2020-12-29T13:05:23","date_gmt":"2020-12-29T05:05:23","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=2614"},"modified":"2020-12-29T13:12:12","modified_gmt":"2020-12-29T05:12:12","slug":"gbase-8a%e6%95%b0%e6%8d%ae%e8%a1%8c%e5%88%97%e8%bd%ac%e6%8d%a2%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/2614","title":{"rendered":"\u5357\u5927\u901a\u7528GBase 8a\u6570\u636e\u884c\u5217\u8f6c\u6362\u7684\u5b9e\u73b0\u65b9\u6cd5"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5e38\u89c1\u7684\u884c\u5217\u8f6c\u6362\u5728GBase8a\u6570\u636e\u5e93\u5185\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Directory Navigation<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2aadd853980\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2aadd853980\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E8%A1%8C%E8%BD%AC%E5%88%97\" >\u884c\u8f6c\u5217<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E6%95%B0%E6%8D%AE\" >\u6570\u636e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E9%80%9A%E8%BF%87case_when%E8%AE%A1%E7%AE%97%E6%96%B0%E7%9A%84%E5%88%97\" >\u901a\u8fc7case when\u8ba1\u7b97\u65b0\u7684\u5217<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E9%80%9A%E8%BF%87%E8%81%9A%E5%90%88%E8%BF%90%E7%AE%97%EF%BC%8C%E6%B1%87%E6%80%BB%E7%BB%93%E6%9E%9C\" >\u901a\u8fc7\u805a\u5408\u8fd0\u7b97\uff0c\u6c47\u603b\u7ed3\u679c<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E5%88%97%E8%BD%AC%E8%A1%8C\" >\u5217\u8f6c\u884c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E6%95%B0%E6%8D%AE-2\" >\u6570\u636e<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.gbase8.cn\/en\/2614\/#%E9%80%9A%E8%BF%87union_all_%E8%BD%AC%E8%A1%8C\" >\u901a\u8fc7union all \u8f6c\u884c<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A1%8C%E8%BD%AC%E5%88%97\"><\/span>\u884c\u8f6c\u5217<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u7c7b\u4f3c\u4ecekv\u7684bigtable\u8f6c\u5316\u6210\u6807\u51c6\u7684\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%95%B0%E6%8D%AE\"><\/span>\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u5b66\u751f\u6bcf\u4e2a\u79d1\u76ee\u7684\u6210\u7ee9\u5355\u6d41\u6c34\uff0c\u8f6c\u5316\u6210\u6bcf\u4e2a\u5b66\u751f\u7684\u6210\u7ee9\u660e\u7ec6\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> create table score(name varchar(100),subject varchar(100),score int);\r\nQuery OK, 0 rows affected (Elapsed: 00:00:00.75)\r\n\r\ngbase> insert into score values ('\u738b\u6d77', '\u8bed\u6587', 86),\r\n    ->         ('\u738b\u6d77', '\u6570\u5b66', 83),\r\n    ->         ('\u738b\u6d77', '\u82f1\u8bed', 93),\r\n    ->         ('\u9676\u4fca', '\u8bed\u6587', 88),\r\n    ->         ('\u9676\u4fca', '\u6570\u5b66', 84),\r\n    ->         ('\u9676\u4fca', '\u82f1\u8bed', 94),\r\n    ->         ('\u5218\u53ef', '\u8bed\u6587', 80),\r\n    ->         ('\u5218\u53ef', '\u6570\u5b66', 86),\r\n    ->         ('\u5218\u53ef', '\u82f1\u8bed', 88),\r\n    ->         ('\u674e\u6625', '\u8bed\u6587', 89),\r\n    ->         ('\u674e\u6625', '\u6570\u5b66', 80),\r\n    ->         ('\u674e\u6625', '\u82f1\u8bed', 87);;\r\nQuery OK, 12 rows affected (Elapsed: 00:00:00.41)\r\nRecords: 12  Duplicates: 0  Warnings: 0\r\n\r\nERROR:\r\nNo query specified\r\n\r\ngbase> select * from score;\r\n+--------+---------+-------+\r\n| name   | subject | score |\r\n+--------+---------+-------+\r\n| \u738b\u6d77   | \u8bed\u6587    |    86 |\r\n| \u738b\u6d77   | \u6570\u5b66    |    83 |\r\n| \u738b\u6d77   | \u82f1\u8bed    |    93 |\r\n| \u9676\u4fca   | \u8bed\u6587    |    88 |\r\n| \u9676\u4fca   | \u6570\u5b66    |    84 |\r\n| \u9676\u4fca   | \u82f1\u8bed    |    94 |\r\n| \u5218\u53ef   | \u8bed\u6587    |    80 |\r\n| \u5218\u53ef   | \u6570\u5b66    |    86 |\r\n| \u5218\u53ef   | \u82f1\u8bed    |    88 |\r\n| \u674e\u6625   | \u8bed\u6587    |    89 |\r\n| \u674e\u6625   | \u6570\u5b66    |    80 |\r\n| \u674e\u6625   | \u82f1\u8bed    |    87 |\r\n+--------+---------+-------+\r\n12 rows in set (Elapsed: 00:00:00.00)\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%80%9A%E8%BF%87case_when%E8%AE%A1%E7%AE%97%E6%96%B0%E7%9A%84%E5%88%97\"><\/span>\u901a\u8fc7case when\u8ba1\u7b97\u65b0\u7684\u5217<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u901a\u8fc7case when, \u5224\u65ad\u5f53\u524d\u5ba2\u6237\u5982\u679c\u662f\u67d0\u4e00\u79cd\u5b66\u79d1\uff0c\u5219\u8ba1\u7b97score\uff0c\u5426\u5219\u8bbe\u7f6e\u4e3a0.\u8fd9\u6837\uff0c\u901a\u8fc7\u4e0d\u540c\u5ba2\u6237\u7684case when\u5c31\u62ff\u5230\u4e86\u6bcf\u4e2a\u5b66\u79d1\u7684\u5206\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select name,\r\nselect *,\r\n    case when subject='\u8bed\u6587' then score else 0 end '\u8bed\u6587' ,\r\n    case when subject='\u6570\u5b66' then score else 0 end '\u6570\u5b66' ,\r\n    case when subject='\u82f1\u8bed' then score else 0 end '\u82f1\u8bed' \r\nfrom score\n\ngbase> select *,\r\n    ->     case when subject='\u8bed\u6587' then score else 0 end '\u8bed\u6587' ,\r\n    ->     case when subject='\u6570\u5b66' then score else 0 end '\u6570\u5b66' ,\r\n    ->     case when subject='\u82f1\u8bed' then score else 0 end '\u82f1\u8bed'\r\n    -> from score;\r\n+--------+---------+-------+--------+--------+--------+\r\n| name   | subject | score | \u8bed\u6587   | \u6570\u5b66   | \u82f1\u8bed   |\r\n+--------+---------+-------+--------+--------+--------+\r\n| \u738b\u6d77   | \u8bed\u6587    |    86 |     86 |      0 |      0 |\r\n| \u738b\u6d77   | \u6570\u5b66    |    83 |      0 |     83 |      0 |\r\n| \u738b\u6d77   | \u82f1\u8bed    |    93 |      0 |      0 |     93 |\r\n| \u9676\u4fca   | \u8bed\u6587    |    88 |     88 |      0 |      0 |\r\n| \u9676\u4fca   | \u6570\u5b66    |    84 |      0 |     84 |      0 |\r\n| \u9676\u4fca   | \u82f1\u8bed    |    94 |      0 |      0 |     94 |\r\n| \u5218\u53ef   | \u8bed\u6587    |    80 |     80 |      0 |      0 |\r\n| \u5218\u53ef   | \u6570\u5b66    |    86 |      0 |     86 |      0 |\r\n| \u5218\u53ef   | \u82f1\u8bed    |    88 |      0 |      0 |     88 |\r\n| \u674e\u6625   | \u8bed\u6587    |    89 |     89 |      0 |      0 |\r\n| \u674e\u6625   | \u6570\u5b66    |    80 |      0 |     80 |      0 |\r\n| \u674e\u6625   | \u82f1\u8bed    |    87 |      0 |      0 |     87 |\r\n+--------+---------+-------+--------+--------+--------+\r\n12 rows in set (Elapsed: 00:00:00.00)\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%80%9A%E8%BF%87%E8%81%9A%E5%90%88%E8%BF%90%E7%AE%97%EF%BC%8C%E6%B1%87%E6%80%BB%E7%BB%93%E6%9E%9C\"><\/span>\u901a\u8fc7\u805a\u5408\u8fd0\u7b97\uff0c\u6c47\u603b\u7ed3\u679c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u7406\u8bba\u4e0a\u4e00\u4e2a\u5b66\u751f\u4e00\u95e8\u529f\u8bfe\u4e0d\u4f1a\u6709\u591a\u4e2a\u6210\u7ee9\uff0c\u8fd9\u91cc\u5c31\u7528max\u51fd\u6570\uff0c\u5373\u4f7f\u6709\u591a\u4e2a\u4e5f\u53d6\u6700\u9ad8\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select name,\r\n    sum(case when subject='\u8bed\u6587' then score else 0 end) '\u8bed\u6587' ,\r\n    sum(case when subject='\u6570\u5b66' then score else 0 end) '\u6570\u5b66' ,\r\n    sum(case when subject='\u82f1\u8bed' then score else 0 end) '\u82f1\u8bed' \r\nfrom score group by name;\n\ngbase> select name,\r\n    ->     sum(case when subject='\u8bed\u6587' then score else 0 end) '\u8bed\u6587' ,\r\n    ->     sum(case when subject='\u6570\u5b66' then score else 0 end) '\u6570\u5b66' ,\r\n    ->     sum(case when subject='\u82f1\u8bed' then score else 0 end) '\u82f1\u8bed'\r\n    -> from score group by name;\r\n+--------+--------+--------+--------+\r\n| name   | \u8bed\u6587   | \u6570\u5b66   | \u82f1\u8bed   |\r\n+--------+--------+--------+--------+\r\n| \u9676\u4fca   |     88 |     84 |     94 |\r\n| \u5218\u53ef   |     80 |     86 |     88 |\r\n| \u674e\u6625   |     89 |     80 |     87 |\r\n| \u738b\u6d77   |     86 |     83 |     93 |\r\n+--------+--------+--------+--------+\r\n4 rows in set (Elapsed: 00:00:00.06)\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%88%97%E8%BD%AC%E8%A1%8C\"><\/span>\u5217\u8f6c\u884c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u7ee7\u7eed\u524d\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u5c06\u5b57\u6bb5\u540d\u6539\u6210\u62fc\u97f3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%95%B0%E6%8D%AE-2\"><\/span>\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>create table score2 as select name,\r\n    sum(case when subject='\u8bed\u6587' then score else 0 end) yuwen ,\r\n    sum(case when subject='\u6570\u5b66' then score else 0 end) shuxue ,\r\n    sum(case when subject='\u82f1\u8bed' then score else 0 end) waiyu \r\nfrom score group by name;\n\ngbase> create table score2 as select name,\r\n    ->     sum(case when subject='\u8bed\u6587' then score else 0 end) yuwen ,\r\n    ->     sum(case when subject='\u6570\u5b66' then score else 0 end) shuxue ,\r\n    ->     sum(case when subject='\u82f1\u8bed' then score else 0 end) waiyu\r\n    -> from score group by name;\r\nQuery OK, 4 rows affected (Elapsed: 00:00:01.33)\r\n\r\ngbase>\r\ngbase>\r\ngbase> select * from score2;\r\n+--------+-------+--------+-------+\r\n| name   | yuwen | shuxue | waiyu |\r\n+--------+-------+--------+-------+\r\n| \u9676\u4fca   |    88 |     84 |    94 |\r\n| \u5218\u53ef   |    80 |     86 |    88 |\r\n| \u674e\u6625   |    89 |     80 |    87 |\r\n| \u738b\u6d77   |    86 |     83 |    93 |\r\n+--------+-------+--------+-------+\r\n4 rows in set (Elapsed: 00:00:00.03)\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%80%9A%E8%BF%87union_all_%E8%BD%AC%E8%A1%8C\"><\/span>\u901a\u8fc7union all \u8f6c\u884c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>select name,'\u8bed\u6587' subject, yuwen score  from score2\r\nunion all\r\nselect name,'\u6570\u5b66' subject, shuxue score  from score2\r\nunion all\r\nselect name,'\u82f1\u8bed' subject, waiyu score  from score2\n\ngbase> select name,'\u8bed\u6587' subject, yuwen score  from score2\r\n    -> union all\r\n    -> select name,'\u6570\u5b66' subject, shuxue score  from score2\r\n    -> union all\r\n    -> select name,'\u82f1\u8bed' subject, waiyu score  from score2;\r\n+--------+---------+-------+\r\n| name   | subject | score |\r\n+--------+---------+-------+\r\n| \u9676\u4fca   | \u8bed\u6587    |    88 |\r\n| \u5218\u53ef   | \u8bed\u6587    |    80 |\r\n| \u674e\u6625   | \u8bed\u6587    |    89 |\r\n| \u9676\u4fca   | \u6570\u5b66    |    84 |\r\n| \u5218\u53ef   | \u6570\u5b66    |    86 |\r\n| \u674e\u6625   | \u6570\u5b66    |    80 |\r\n| \u9676\u4fca   | \u82f1\u8bed    |    94 |\r\n| \u5218\u53ef   | \u82f1\u8bed    |    88 |\r\n| \u674e\u6625   | \u82f1\u8bed    |    87 |\r\n| \u738b\u6d77   | \u8bed\u6587    |    86 |\r\n| \u738b\u6d77   | \u6570\u5b66    |    83 |\r\n| \u738b\u6d77   | \u82f1\u8bed    |    93 |\r\n+--------+---------+-------+\r\n12 rows in set (Elapsed: 00:00:00.00)\r\n<\/code><\/pre>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5e38\u89c1\u7684\u884c\u5217\u8f6c\u6362\u5728GBase8a\u6570\u636e\u5e93\u5185\u5b9e\u73b0\u7684\u65b9\u6cd5<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2614","post","type-post","status-publish","format-standard","hentry","category-gbase8a"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/2614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/comments?post=2614"}],"version-history":[{"count":2,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/2614\/revisions"}],"predecessor-version":[{"id":2617,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/2614\/revisions\/2617"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=2614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=2614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=2614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}