{"id":961,"date":"2025-01-15T00:36:24","date_gmt":"2025-01-14T16:36:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=961"},"modified":"2025-01-15T00:36:24","modified_gmt":"2025-01-14T16:36:24","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-t-sql-%e4%b8%ad%e6%89%a7%e8%a1%8c%e5%8a%a8%e6%80%81%e9%80%8f%e8%a7%86%ef%bc%9f%e6%ad%a5%e9%aa%a4%e3%80%81%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7%e4%b8%8e%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/15\/%e5%a6%82%e4%bd%95%e5%9c%a8-t-sql-%e4%b8%ad%e6%89%a7%e8%a1%8c%e5%8a%a8%e6%80%81%e9%80%8f%e8%a7%86%ef%bc%9f%e6%ad%a5%e9%aa%a4%e3%80%81%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7%e4%b8%8e%e5%ae%9e%e6%88%98\/","title":{"rendered":"\u5982\u4f55\u5728 T-SQL \u4e2d\u6267\u884c\u52a8\u6001\u900f\u89c6\uff1f\u6b65\u9aa4\u3001\u4f18\u5316\u6280\u5de7\u4e0e\u5b9e\u6218\u793a\u4f8b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 T-SQL \u4e2d\uff0c\u900f\u89c6\uff08Pivot\uff09\u662f\u5c06\u6570\u636e\u4ece\u884c\u8f6c\u5316\u4e3a\u5217\u7684\u64cd\u4f5c\uff0c\u4f7f\u5f97\u6570\u636e\u5c55\u793a\u66f4\u52a0\u6e05\u6670\u3001\u6613\u4e8e\u5206\u6790\u3002\u52a8\u6001\u900f\u89c6\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5f53\u4f60\u4e0d\u786e\u5b9a\u900f\u89c6\u64cd\u4f5c\u7684\u5217\u540d\u65f6\uff08\u4f8b\u5982\uff0c\u5217\u540d\u4e0d\u56fa\u5b9a\uff0c\u53ef\u80fd\u4f1a\u968f\u7740\u6570\u636e\u53d8\u5316\u800c\u6539\u53d8\uff09\u8fdb\u884c\u6570\u636e\u8f6c\u7f6e\u3002\u4e0e\u9759\u6001\u900f\u89c6\u4e0d\u540c\uff0c\u52a8\u6001\u900f\u89c6\u9700\u8981\u7f16\u5199\u52a8\u6001 SQL \u8bed\u53e5\u6765\u9002\u5e94\u4e0d\u540c\u7684\u5217\u540d\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u900f\u89c6\u4e0e\u52a8\u6001\u900f\u89c6\u7684\u533a\u522b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9759\u6001\u900f\u89c6<\/strong>\uff1a\u5f53\u5217\u540d\u5df2\u77e5\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>PIVOT<\/code> \u64cd\u4f5c\u7b26\u8fdb\u884c\u884c\u5217\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong>\u52a8\u6001\u900f\u89c6<\/strong>\uff1a\u5f53\u5217\u540d\u4e0d\u56fa\u5b9a\u6216\u5217\u540d\u9700\u8981\u4ece\u6570\u636e\u4e2d\u52a8\u6001\u751f\u6210\u65f6\uff0c\u4f7f\u7528\u52a8\u6001 SQL \u6765\u6784\u9020\u900f\u89c6\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u900f\u89c6\u901a\u5e38\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5148\u67e5\u8be2\u51fa\u9700\u8981\u900f\u89c6\u7684\u5217\u540d\u3002<\/li>\n\n\n\n<li>\u6839\u636e\u8fd9\u4e9b\u5217\u540d\u6784\u5efa\u52a8\u6001 SQL\u3002<\/li>\n\n\n\n<li>\u6267\u884c\u52a8\u6001 SQL \u8fdb\u884c\u900f\u89c6\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u9759\u6001\u900f\u89c6\u7684\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u89e3\u91ca\u52a8\u6001\u900f\u89c6\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u9759\u6001\u900f\u89c6\u7684\u7b80\u5355\u4f8b\u5b50\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a <code>SalesData<\/code> \u8868\uff0c\u5305\u542b\u5b57\u6bb5 <code>Product<\/code>, <code>Year<\/code>, <code>Amount<\/code>\uff0c\u6211\u4eec\u60f3\u5c06\u6570\u636e\u6309\u5e74\u4efd\u900f\u89c6\uff0c\u4f7f\u6bcf\u4e2a\u5e74\u4efd\u6210\u4e3a\u5217\u540d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8868\u7ed3\u6784\uff1a<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Product<\/th><th>Year<\/th><th>Amount<\/th><\/tr><\/thead><tbody><tr><td>ProductA<\/td><td>2020<\/td><td>100<\/td><\/tr><tr><td>ProductB<\/td><td>2020<\/td><td>150<\/td><\/tr><tr><td>ProductA<\/td><td>2021<\/td><td>200<\/td><\/tr><tr><td>ProductB<\/td><td>2021<\/td><td>250<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u9759\u6001\u900f\u89c6\u67e5\u8be2\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT Product,\n       &#91;2020], \n       &#91;2021]\nFROM (SELECT Product, Year, Amount FROM SalesData) AS SourceTable\nPIVOT (\n    SUM(Amount)\n    FOR Year IN (&#91;2020], &#91;2021])\n) AS PivotTable;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ed3\u679c\uff1a<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Product<\/th><th>2020<\/th><th>2021<\/th><\/tr><\/thead><tbody><tr><td>ProductA<\/td><td>100<\/td><td>200<\/td><\/tr><tr><td>ProductB<\/td><td>150<\/td><td>250<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u9759\u6001\u900f\u89c6\u7684\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\uff0c\u4f46\u95ee\u9898\u5728\u4e8e\u5982\u679c\u5217\uff08\u5982\u5e74\u4efd\uff09\u662f\u52a8\u6001\u53d8\u5316\u7684\uff0c\u9759\u6001\u65b9\u5f0f\u5c31\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u4e86\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u4f7f\u7528\u52a8\u6001\u900f\u89c6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u52a8\u6001\u900f\u89c6\u7684\u6b65\u9aa4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u52a8\u6001\u900f\u89c6\u4e2d\uff0c\u9996\u5148\u9700\u8981\u786e\u5b9a\u900f\u89c6\u5217\u540d\u7684\u6765\u6e90\uff0c\u7136\u540e\u52a8\u6001\u751f\u6210 SQL \u67e5\u8be2\u8bed\u53e5\uff0c\u6700\u540e\u6267\u884c\u67e5\u8be2\u3002\u4ee5\u4e0b\u662f\u8fdb\u884c\u52a8\u6001\u900f\u89c6\u7684\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 1\uff1a\u67e5\u8be2\u52a8\u6001\u5217\u540d<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u4ecd\u7136\u4f7f\u7528\u4e0a\u9762\u7684 <code>SalesData<\/code> \u8868\uff0c\u4f46\u6211\u4eec\u4e0d\u77e5\u9053\u5c06\u6765\u53ef\u80fd\u4f1a\u6709\u54ea\u4e9b\u5e74\u4efd\uff08\u5373\u5217\u540d\uff09\u3002\u6211\u4eec\u9996\u5148\u9700\u8981\u67e5\u8be2\u6240\u6709\u53ef\u80fd\u7684\u5e74\u4efd\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5217\u540d\u6765\u900f\u89c6\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX);\n\n-- \u83b7\u53d6\u6240\u6709\u7684\u5e74\u4efd\u5217\u540d\nSELECT @columns = STRING_AGG(QUOTENAME(Year), ', ') \nFROM (SELECT DISTINCT Year FROM SalesData) AS Years;\n\n-- \u52a8\u6001\u6784\u9020 SQL \u67e5\u8be2\nSET @sql = '\nSELECT Product, ' + @columns + ' \nFROM \n    (SELECT Product, Year, Amount FROM SalesData) AS SourceTable\nPIVOT (\n    SUM(Amount)\n    FOR Year IN (' + @columns + ')\n) AS PivotTable;';\n\n-- \u6267\u884c\u52a8\u6001 SQL\nEXEC sp_executesql @sql;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 2\uff1a\u89e3\u91ca\u4ee3\u7801<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u8be2\u5217\u540d<\/strong>\uff1a\u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528 <code>STRING_AGG()<\/code> \u51fd\u6570\u5c06\u6240\u6709\u7684\u5e74\u4efd\u5217\u540d\uff08<code>Year<\/code> \u5b57\u6bb5\uff09\u62fc\u63a5\u6210\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002\u6ce8\u610f\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86 <code>QUOTENAME()<\/code> \u51fd\u6570\u6765\u786e\u4fdd\u5217\u540d\u4e2d\u53ef\u80fd\u5b58\u5728\u7684\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u7a7a\u683c\uff09\u88ab\u9002\u5f53\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u6784\u5efa\u52a8\u6001 SQL<\/strong>\uff1a\u4f7f\u7528\u67e5\u8be2\u5230\u7684\u5e74\u4efd\u5217\u540d\uff0c\u52a8\u6001\u6784\u9020\u4e00\u4e2a\u5b8c\u6574\u7684 SQL \u67e5\u8be2\u3002<\/li>\n\n\n\n<li><strong>\u6267\u884c\u52a8\u6001 SQL<\/strong>\uff1a\u4f7f\u7528 <code>sp_executesql<\/code> \u6267\u884c\u6784\u9020\u597d\u7684\u52a8\u6001 SQL \u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b65\u9aa4 3\uff1a\u7ed3\u679c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe <code>SalesData<\/code> \u8868\u5305\u542b\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Product<\/th><th>Year<\/th><th>Amount<\/th><\/tr><\/thead><tbody><tr><td>ProductA<\/td><td>2020<\/td><td>100<\/td><\/tr><tr><td>ProductB<\/td><td>2020<\/td><td>150<\/td><\/tr><tr><td>ProductA<\/td><td>2021<\/td><td>200<\/td><\/tr><tr><td>ProductB<\/td><td>2021<\/td><td>250<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u900f\u89c6\u7684\u67e5\u8be2\u5c06\u8fd4\u56de\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Product<\/th><th>2020<\/th><th>2021<\/th><\/tr><\/thead><tbody><tr><td>ProductA<\/td><td>100<\/td><td>200<\/td><\/tr><tr><td>ProductB<\/td><td>150<\/td><td>250<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u52a8\u6001\u900f\u89c6\u7684\u4f18\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u900f\u89c6\u901a\u8fc7 <code>sp_executesql<\/code> \u6267\u884c\u751f\u6210\u7684 SQL \u67e5\u8be2\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u4e2a\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u8fc7\u5ea6\u4f9d\u8d56\u52a8\u6001 SQL<\/strong>\uff1a\u5982\u679c\u900f\u89c6\u7684\u5217\u6570\u8f83\u4e3a\u56fa\u5b9a\uff0c\u5c3d\u91cf\u4f7f\u7528\u9759\u6001\u900f\u89c6\uff08<code>PIVOT<\/code> \u64cd\u4f5c\u7b26\uff09\uff0c\u8fd9\u901a\u5e38\u66f4\u9ad8\u6548\u3002<\/li>\n\n\n\n<li><strong>\u7d22\u5f15\u4f18\u5316<\/strong>\uff1a\u786e\u4fdd\u53c2\u4e0e\u900f\u89c6\u7684\u8868\uff08\u5982 <code>SalesData<\/code>\uff09\u6709\u9002\u5f53\u7684\u7d22\u5f15\uff0c\u7279\u522b\u662f\u5728\u53c2\u4e0e\u7b5b\u9009\u548c\u805a\u5408\u64cd\u4f5c\u7684\u5217\u4e0a\u3002<\/li>\n\n\n\n<li><strong>\u5206\u6279\u67e5\u8be2<\/strong>\uff1a\u5982\u679c\u900f\u89c6\u7684\u5217\u6570\u975e\u5e38\u5e9e\u5927\uff0c\u8003\u8651\u5c06\u52a8\u6001\u900f\u89c6\u5206\u6279\u6267\u884c\u6216\u8fdb\u884c\u5206\u9875\u67e5\u8be2\uff0c\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u7684\u9501\u5b9a\u548c\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6267\u884c\u52a8\u6001 SQL \u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5217\u540d\u5305\u542b\u7279\u6b8a\u5b57\u7b26<\/strong>\uff1a\u786e\u4fdd\u4f7f\u7528 <code>QUOTENAME()<\/code> \u5305\u88f9\u52a8\u6001\u5217\u540d\uff0c\u4ee5\u907f\u514d SQL \u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u7a7a\u503c\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u8868\u4e2d\u6ca1\u6709\u6570\u636e\u6216\u67d0\u4e9b\u5e74\u4efd\u7684\u6570\u636e\u4e3a\u7a7a\uff0c\u786e\u4fdd\u9002\u5f53\u5730\u5904\u7406 <code>NULL<\/code> \u503c\uff0c\u4ee5\u907f\u514d\u8f93\u51fa\u4e0d\u51c6\u786e\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u900f\u89c6\u5728 T-SQL \u4e2d\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u5c06\u884c\u6570\u636e\u8f6c\u5316\u4e3a\u5217\u3002\u5c24\u5176\u5f53\u5217\u540d\u52a8\u6001\u53d8\u5316\u65f6\uff0c\u4f7f\u7528\u52a8\u6001 SQL \u6784\u5efa\u900f\u89c6\u67e5\u8be2\u53d8\u5f97\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u5229\u7528 <code>STRING_AGG()<\/code>\u3001<code>QUOTENAME()<\/code> \u548c <code>sp_executesql<\/code> \u7b49\u529f\u80fd\uff0c\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u5730\u5e94\u5bf9\u8fd9\u4e00\u9700\u6c42\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a8\u6001\u900f\u89c6\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u8f6c\u6362\u65b9\u5f0f\uff0c\u80fd\u591f\u9002\u5e94\u6570\u636e\u7684\u4e0d\u65ad\u53d8\u5316\uff0c\u4f46\u4e5f\u9700\u8981\u5408\u7406\u7684\u6027\u80fd\u548c\u9519\u8bef\u7ba1\u7406\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 T-SQL \u4e2d\uff0c\u900f\u89c6\uff08Pivot\uff09\u662f\u5c06\u6570\u636e\u4ece\u884c\u8f6c\u5316\u4e3a\u5217\u7684\u64cd\u4f5c\uff0c\u4f7f\u5f97\u6570\u636e\u5c55\u793a\u66f4\u52a0\u6e05\u6670\u3001\u6613\u4e8e\u5206\u6790\u3002\u52a8\u6001\u900f\u89c6\u662f\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-961","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=961"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/961\/revisions"}],"predecessor-version":[{"id":962,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/961\/revisions\/962"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}