{"id":5299,"date":"2024-11-14T09:48:14","date_gmt":"2024-11-14T01:48:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5299\/"},"modified":"2024-11-14T09:48:14","modified_gmt":"2024-11-14T01:48:14","slug":"oracle%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5299\/","title":{"rendered":"oracle\u600e\u4e48\u8f6c\u6362\u4e3a\u5217"},"content":{"rendered":"<blockquote><p>\n  oracle \u6570\u636e\u5e93\u7684 pivot \u51fd\u6570\u53ef\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff0c\u8bed\u6cd5\u4e3a\uff1apivot (agg_expr for col_name in (value1, value2, &#8230;, valuen) [partition by partition_col_name1, &#8230;, partition_col_namen])\u3002\u793a\u4f8b\u4f7f\u7528\u573a\u666f\u662f\u5c06\u591a\u4e2a\u884c\u7684 sport \u5217\u8f6c\u6362\u4e3a\u5bf9\u5e94\u5217\uff0c\u5982 basketball\u3001soccer\u3001baseball\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052618483734449.jpg\" class=\"aligncenter\" title=\"oracle\u600e\u4e48\u8f6c\u6362\u4e3a\u5217\u63d2\u56fe\" alt=\"oracle\u600e\u4e48\u8f6c\u6362\u4e3a\u5217\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Oracle \u6570\u636e\u8f6c\u6362\u6210\u5217<\/strong><\/p>\n<p>Oracle \u6570\u636e\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a PIVOT \u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\u3002PIVOT \u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>PIVOT (\n    agg_expr\n    FOR col_name\n    IN (value1, value2, ..., valueN)\n    [PARTITION BY partition_col_name1, ..., partition_col_nameN]\n)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5c06\u4ee5\u4e0b\u8868\u4e2d\u7684\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>ID<\/th>\n<th>Name<\/th>\n<th>Sport<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>John<\/td>\n<td>Basketball<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Mary<\/td>\n<td>Soccer<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Bob<\/td>\n<td>Basketball<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Jane<\/td>\n<td>Soccer<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Tom<\/td>\n<td>Baseball<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f7f\u7528 PIVOT \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff1a<\/p>\n<pre>SELECT ID,\n       MAX(CASE WHEN Sport = 'Basketball' THEN Name END) AS Basketball,\n       MAX(CASE WHEN Sport = 'Soccer' THEN Name END) AS Soccer,\n       MAX(CASE WHEN Sport = 'Baseball' THEN Name END) AS Baseball\nFROM (\n    SELECT ID, Name, Sport\n    FROM table_name\n)\nPIVOT (\n    Name\n    FOR Sport\n    IN ('Basketball', 'Soccer', 'Baseball')\n)\nGROUP BY ID;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>ID<\/th>\n<th>Basketball<\/th>\n<th>Soccer<\/th>\n<th>Baseball<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>John<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>NULL<\/td>\n<td>Mary<\/td>\n<td>NULL<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Bob<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>NULL<\/td>\n<td>Jane<\/td>\n<td>NULL<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>Tom<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u8f6c\u6362\u4e3a\u5217\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>oracle \u6570\u636e\u5e93\u7684 pivot \u51fd\u6570\u53ef\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff0c\u8bed\u6cd5\u4e3a\uff1apivot (agg_expr for col_name in (value1, value2, &#8230;, valuen) [partition by partition_col_name1, &#8230;, partition_col_namen])\u3002\u793a\u4f8b\u4f7f\u7528\u573a\u666f\u662f\u5c06\u591a\u4e2a\u884c\u7684 sport \u5217\u8f6c\u6362\u4e3a\u5bf9\u5e94\u5217\uff0c\u5982 basketball\u3001soccer\u3001baseball\u3002 \u5982\u4f55\u5c06 Oracle \u6570\u636e\u8f6c\u6362\u6210\u5217 Oracle \u6570\u636e\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a PIVOT \u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\u3002PIVOT \u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a PIVOT ( agg_expr FOR col_name IN (value1, value2, &#8230;, valueN) [PARTITION BY partition_col_name1, &#8230;, partition_col_nameN] ) \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u5c06\u4ee5\u4e0b\u8868\u4e2d\u7684\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff1a ID Name Sport 1 John Basketball 2 Mary [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5299","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5299"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5299\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}