{"id":139598,"date":"2024-10-31T18:53:23","date_gmt":"2024-10-31T10:53:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/139598\/"},"modified":"2024-10-31T18:53:24","modified_gmt":"2024-10-31T10:53:24","slug":"sql-server%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a6%82%e4%bd%95%e6%8c%89%e7%99%be%e5%88%86%e6%af%94%e6%9f%a5%e8%a9%a2%e5%87%ba%e8%a1%a8%e4%b8%ad%e7%9a%84%e8%a8%98%e9%8c%84%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/139598\/","title":{"rendered":"SQL Server\u6578\u64da\u5eab\u5982\u4f55\u6309\u767e\u5206\u6bd4\u67e5\u8a62\u51fa\u8868\u4e2d\u7684\u8a18\u9304\u6578"},"content":{"rendered":"<h1 id=\"sql-server%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a6%82%e4%bd%95%e6%8c%89%e7%99%be%e5%88%86%e6%af%94%e6%9f%a5%e8%a9%a2%e5%87%ba%e8%a1%a8%e4%b8%ad%e7%9a%84%e8%a8%98%e9%8c%84%e6%95%b8-UaLIVoWGGU\">SQL Server\u6578\u64da\u5eab\u5982\u4f55\u6309\u767e\u5206\u6bd4\u67e5\u8a62\u51fa\u8868\u4e2d\u7684\u8a18\u9304\u6578<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cSQL Server\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u9700\u8981\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6578\u64da\uff0c\u4e26\u6839\u64da\u67d0\u4e9b\u689d\u4ef6\u9032\u884c\u5206\u6790\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728SQL Server\u4e2d\u6309\u767e\u5206\u6bd4\u67e5\u8a62\u51fa\u8868\u4e2d\u7684\u8a18\u9304\u6578\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u5206\u6790\u548c\u5831\u544a\u751f\u6210\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e7%90%86%e8%a7%a3%e8%a8%98%e9%8c%84%e6%95%b8%e7%9a%84%e7%99%be%e5%88%86%e6%af%94%e6%9f%a5%e8%a9%a2-UaLIVoWGGU\">\u7406\u89e3\u8a18\u9304\u6578\u7684\u767e\u5206\u6bd4\u67e5\u8a62<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0c\u67e5\u8a62\u8868\u4e2d\u7684\u8a18\u9304\u6578\u901a\u5e38\u662f\u901a\u904e\u4f7f\u7528\u805a\u5408\u51fd\u6578\u4f86\u5be6\u73fe\u7684\u3002\u7576\u6211\u5011\u9700\u8981\u6309\u767e\u5206\u6bd4\u4f86\u67e5\u8a62\u8a18\u9304\u6578\u6642\uff0c\u9996\u5148\u9700\u8981\u78ba\u5b9a\u7e3d\u8a18\u9304\u6578\uff0c\u7136\u5f8c\u6839\u64da\u7279\u5b9a\u689d\u4ef6\u8a08\u7b97\u7b26\u5408\u689d\u4ef6\u7684\u8a18\u9304\u6578\uff0c\u6700\u5f8c\u5c07\u9019\u5169\u8005\u9032\u884c\u6bd4\u8f03\u4ee5\u7372\u5f97\u767e\u5206\u6bd4\u3002<\/p>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e6%9f%a5%e8%a9%a2%e7%a4%ba%e4%be%8b-UaLIVoWGGU\">\u57fa\u672c\u67e5\u8a62\u793a\u4f8b<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>Employees<\/code> \u7684\u8868\uff0c\u8a72\u8868\u5305\u542b\u54e1\u5de5\u7684\u8a73\u7d30\u4fe1\u606f\uff0c\u5305\u62ec\u4ed6\u5011\u7684\u90e8\u9580\u548c\u85aa\u6c34\u3002\u6211\u5011\u5e0c\u671b\u67e5\u8a62\u67d0\u4e00\u90e8\u9580\u7684\u54e1\u5de5\u6578\u91cf\u4f54\u7e3d\u54e1\u5de5\u6578\u91cf\u7684\u767e\u5206\u6bd4\u3002<\/p>\n<pre><code>SELECT \n    COUNT(*) AS TotalEmployees,\n    (SELECT COUNT(*) FROM Employees WHERE Department = 'Sales') AS SalesEmployees,\n    (SELECT COUNT(*) FROM Employees WHERE Department = 'Sales') * 100.0 \/ COUNT(*) AS SalesPercentage\nFROM Employees;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u67e5\u8a62\u4e2d\uff0c\u6211\u5011\u9996\u5148\u8a08\u7b97\u7e3d\u54e1\u5de5\u6578 <code>TotalEmployees<\/code>\uff0c\u7136\u5f8c\u8a08\u7b97\u92b7\u552e\u90e8\u9580\u7684\u54e1\u5de5\u6578 <code>SalesEmployees<\/code>\uff0c\u6700\u5f8c\u8a08\u7b97\u92b7\u552e\u90e8\u9580\u54e1\u5de5\u6578\u4f54\u7e3d\u54e1\u5de5\u6578\u7684\u767e\u5206\u6bd4 <code>SalesPercentage<\/code>\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8cte%e9%80%b2%e8%a1%8c%e6%9b%b4%e8%a4%87%e9%9b%9c%e7%9a%84%e6%9f%a5%e8%a9%a2-UaLIVoWGGU\">\u4f7f\u7528CTE\u9032\u884c\u66f4\u8907\u96dc\u7684\u67e5\u8a62<\/h2>\n<p>\u5c0d\u65bc\u66f4\u8907\u96dc\u7684\u67e5\u8a62\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u516c\u5171\u8868\u8868\u9054\u5f0f\uff08CTE\uff09\u4f86\u63d0\u9ad8\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528CTE\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>WITH EmployeeCounts AS (\n    SELECT \n        COUNT(*) AS TotalEmployees,\n        COUNT(CASE WHEN Department = 'Sales' THEN 1 END) AS SalesEmployees\n    FROM Employees\n)\nSELECT \n    TotalEmployees,\n    SalesEmployees,\n    SalesEmployees * 100.0 \/ TotalEmployees AS SalesPercentage\nFROM EmployeeCounts;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5728CTE\u4e2d\u8a08\u7b97\u7e3d\u54e1\u5de5\u6578\u548c\u92b7\u552e\u90e8\u9580\u7684\u54e1\u5de5\u6578\uff0c\u7136\u5f8c\u5728\u4e3b\u67e5\u8a62\u4e2d\u8a08\u7b97\u767e\u5206\u6bd4\u3002\u9019\u7a2e\u65b9\u6cd5\u4f7f\u5f97\u67e5\u8a62\u7d50\u69cb\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<h2 id=\"%e8%80%83%e6%85%ae%e6%95%b8%e6%93%9a%e7%9a%84%e7%af%a9%e9%81%b8%e6%a2%9d%e4%bb%b6-UaLIVoWGGU\">\u8003\u616e\u6578\u64da\u7684\u7be9\u9078\u689d\u4ef6<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u6211\u5011\u53ef\u80fd\u9700\u8981\u6839\u64da\u5176\u4ed6\u689d\u4ef6\u4f86\u7be9\u9078\u6578\u64da\u3002\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u80fd\u53ea\u60f3\u67e5\u8a62\u67d0\u500b\u7279\u5b9a\u5e74\u4efd\u5165\u8077\u7684\u54e1\u5de5\u3002\u9019\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u67e5\u8a62\u4e2d\u6dfb\u52a0 <code>WHERE<\/code> \u5b50\u53e5\uff1a<\/p>\n<pre><code>WITH EmployeeCounts AS (\n    SELECT \n        COUNT(*) AS TotalEmployees,\n        COUNT(CASE WHEN Department = 'Sales' THEN 1 END) AS SalesEmployees\n    FROM Employees\n    WHERE YearOfJoining = 2023\n)\nSELECT \n    TotalEmployees,\n    SalesEmployees,\n    SalesEmployees * 100.0 \/ TotalEmployees AS SalesPercentage\nFROM EmployeeCounts;<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u80fd\u5920\u7372\u5f97\u7279\u5b9a\u689d\u4ef6\u4e0b\u7684\u8a18\u9304\u6578\u767e\u5206\u6bd4\uff0c\u9019\u5c0d\u65bc\u5206\u6790\u7279\u5b9a\u6642\u9593\u6bb5\u5167\u7684\u6578\u64da\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-UaLIVoWGGU\">\u7e3d\u7d50<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0c\u6309\u767e\u5206\u6bd4\u67e5\u8a62\u8868\u4e2d\u7684\u8a18\u9304\u6578\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\uff0c\u7121\u8ad6\u662f\u7528\u65bc\u6578\u64da\u5206\u6790\u9084\u662f\u5831\u544a\u751f\u6210\u3002\u901a\u904e\u4f7f\u7528\u805a\u5408\u51fd\u6578\u548cCTE\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u8a08\u7b97\u51fa\u7b26\u5408\u7279\u5b9a\u689d\u4ef6\u7684\u8a18\u9304\u6578\u4f54\u7e3d\u8a18\u9304\u6578\u7684\u767e\u5206\u6bd4\u3002\u9019\u4e9b\u6280\u8853\u4e0d\u50c5\u63d0\u9ad8\u4e86\u67e5\u8a62\u7684\u6548\u7387\uff0c\u9084\u589e\u5f37\u4e86\u6578\u64da\u5206\u6790\u7684\u9748\u6d3b\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u696d\u52d9\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728SQL Server\u6578\u64da\u5eab\u4e2d\u6309\u767e\u5206\u6bd4\u67e5\u8a62\u8868\u4e2d\u7684\u8a18\u9304\u6578\uff0c\u8f15\u9b06\u7372\u53d6\u6240\u9700\u6578\u64da\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-139598","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=139598"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139598\/revisions"}],"predecessor-version":[{"id":139599,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/139598\/revisions\/139599"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=139598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=139598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=139598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}