{"id":965,"date":"2025-01-15T00:38:34","date_gmt":"2025-01-14T16:38:34","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=965"},"modified":"2025-01-15T00:38:34","modified_gmt":"2025-01-14T16:38:34","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-countdistinct-%e8%ae%a1%e7%ae%97-sql-server-%e4%b8%ad%e4%b8%8d%e5%90%8c%e7%a8%8b%e5%ba%8f%e5%90%8d%e7%a7%b0%e7%9a%84%e6%95%b0%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/15\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-countdistinct-%e8%ae%a1%e7%ae%97-sql-server-%e4%b8%ad%e4%b8%8d%e5%90%8c%e7%a8%8b%e5%ba%8f%e5%90%8d%e7%a7%b0%e7%9a%84%e6%95%b0%e9%87%8f%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 COUNT(DISTINCT) \u8ba1\u7b97 SQL Server \u4e2d\u4e0d\u540c\u7a0b\u5e8f\u540d\u79f0\u7684\u6570\u91cf\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 SQL Server \u4e2d\uff0c<code>COUNT(DISTINCT)<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u5b83\u7528\u4e8e\u8ba1\u7b97\u67d0\u4e2a\u5b57\u6bb5\u4e2d\u4e0d\u91cd\u590d\u7684\u4e0d\u540c\u503c\u7684\u6570\u91cf\u3002\u8fd9\u4e2a\u529f\u80fd\u5e38\u5e38\u7528\u4e8e\u67e5\u8be2\u4e0d\u540c\u7c7b\u522b\u3001\u4e0d\u540c\u9879\u76ee\u3001\u6216\u8005\u4e0d\u540c\u7684\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u4e86\u89e3\u67d0\u4e2a\u5b57\u6bb5\u4e2d\u6709\u591a\u5c11\u72ec\u7279\u7684\u503c\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u7a0b\u5e8f\u540d\u79f0\uff08\u4f8b\u5982 <code>program_name<\/code>\uff09\u7684\u8868\u683c\uff0c\u5e76\u4e14\u4f60\u5e0c\u671b\u8ba1\u7b97\u8868\u4e2d\u4e0d\u540c\u7684\u7a0b\u5e8f\u540d\u79f0\u7684\u6570\u91cf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>COUNT(DISTINCT)<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u573a\u666f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>program_logs<\/code> \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b <code>program_name<\/code> \u548c <code>execution_time<\/code> \u5b57\u6bb5\uff0c\u8bb0\u5f55\u4e86\u7a0b\u5e8f\u7684\u540d\u79f0\u4ee5\u53ca\u5b83\u4eec\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f60\u5e0c\u671b\u8ba1\u7b97\u8868\u4e2d\u51fa\u73b0\u8fc7\u591a\u5c11\u4e0d\u540c\u7684\u7a0b\u5e8f\u540d\u79f0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b SQL \u67e5\u8be2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(DISTINCT program_name) AS unique_program_count\nFROM program_logs;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT(DISTINCT program_name)<\/code> \u4f1a\u8ba1\u7b97 <code>program_logs<\/code> \u8868\u4e2d\u6240\u6709\u4e0d\u91cd\u590d\u7684 <code>program_name<\/code> \u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li><code>AS unique_program_count<\/code> \u662f\u5bf9\u8ba1\u7b97\u7ed3\u679c\u7684\u522b\u540d\uff0c\u8868\u793a\u8ba1\u7b97\u7ed3\u679c\u5c06\u663e\u793a\u4e3a <code>unique_program_count<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u8be6\u7ec6\u89e3\u91ca<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>COUNT(DISTINCT)<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT(DISTINCT)<\/code> \u662f\u4e00\u4e2a\u805a\u5408\u51fd\u6570\uff0c\u7528\u6765\u8ba1\u7b97\u67d0\u4e00\u5217\u4e2d\u4e0d\u91cd\u590d\u7684\u503c\u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c\u5047\u8bbe <code>program_name<\/code> \u5217\u5305\u542b\u5982\u4e0b\u6570\u636e\uff1a<code>'ProgramA', 'ProgramB', 'ProgramA', 'ProgramC'<\/code>\uff0c\u90a3\u4e48 <code>COUNT(DISTINCT program_name)<\/code> \u8fd4\u56de\u7684\u7ed3\u679c\u662f 3\uff0c\u56e0\u4e3a <code>'ProgramA'<\/code> \u53ea\u7b97\u4f5c\u4e00\u4e2a\u4e0d\u540c\u7684\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT(DISTINCT)<\/code> \u5e38\u5e38\u7528\u4e8e\u67e5\u627e\u72ec\u7279\u7684\u503c\u4e2a\u6570\uff0c\u5c24\u5176\u5728\u9700\u8981\u5206\u6790\u591a\u6b21\u6267\u884c\u7684\u8bb0\u5f55\u3001\u552f\u4e00\u6807\u8bc6\u7b26\u7684\u8ba1\u6570\u3001\u4ee5\u53ca\u53bb\u91cd\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n\n\n\n<li>\u5728\u7a0b\u5e8f\u65e5\u5fd7\u3001\u8ba2\u5355\u8bb0\u5f55\u7b49\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u77e5\u9053\u6709\u591a\u5c11\u4e0d\u540c\u7684\u9879\u76ee\u3001\u7528\u6237\u6216\u7a0b\u5e8f\u53c2\u4e0e\u4e86\u67d0\u4e9b\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u867d\u7136 <code>COUNT(DISTINCT)<\/code> \u662f\u975e\u5e38\u76f4\u89c2\u4e14\u6709\u6548\u7684\uff0c\u4f46\u5728\u5927\u6570\u636e\u91cf\u4e0b\uff0c\u5b83\u53ef\u80fd\u4f1a\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\uff0c\u56e0\u4e3a SQL Server \u9700\u8981\u5bf9\u6240\u6709\u6570\u636e\u8fdb\u884c\u53bb\u91cd\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u4f18\u5316\u67e5\u8be2\uff0c\u7279\u522b\u662f\u5728\u7528\u4e8e\u53bb\u91cd\u7684\u5217\u4e0a\u521b\u5efa\u7d22\u5f15\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fdb\u9636\u5e94\u7528<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>\u6309\u67d0\u4e2a\u5b57\u6bb5\u5206\u7ec4\u7edf\u8ba1\u4e0d\u540c\u7a0b\u5e8f\u540d\u79f0<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u6309\u5176\u4ed6\u5b57\u6bb5\uff08\u4f8b\u5982 <code>execution_time<\/code>\uff09\u5bf9\u4e0d\u540c\u7a0b\u5e8f\u540d\u79f0\u7684\u6570\u91cf\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>GROUP BY<\/code> \u5b50\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT execution_time, COUNT(DISTINCT program_name) AS unique_program_count\nFROM program_logs\nGROUP BY execution_time;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u5c06\u6309 <code>execution_time<\/code> \u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u4e0d\u540c\u7a0b\u5e8f\u540d\u79f0\u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u7ed3\u5408\u5176\u4ed6\u805a\u5408\u51fd\u6570<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u805a\u5408\u51fd\u6570\uff0c\u6bd4\u5982\u8ba1\u7b97\u67d0\u4e2a\u5b57\u6bb5\u7684\u6700\u5927\u503c\u6216\u5e73\u5747\u503c\uff0c\u5e76\u4e14\u540c\u65f6\u7edf\u8ba1\u4e0d\u91cd\u590d\u7684\u7a0b\u5e8f\u540d\u79f0\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT execution_time, COUNT(DISTINCT program_name) AS unique_program_count, MAX(execution_time) AS max_execution_time\nFROM program_logs\nGROUP BY execution_time;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SQL Server \u4e2d\uff0c\u4f7f\u7528 <code>COUNT(DISTINCT)<\/code> \u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e0d\u540c\u7a0b\u5e8f\u540d\u79f0\u7684\u6570\u91cf\u3002\u65e0\u8bba\u662f\u5728\u7b80\u5355\u67e5\u8be2\u4e2d\uff0c\u8fd8\u662f\u5728\u590d\u6742\u7684\u5206\u7ec4\u7edf\u8ba1\u4e2d\uff0c<code>COUNT(DISTINCT)<\/code> \u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u7684\u67e5\u8be2\u8bbe\u8ba1\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u83b7\u53d6\u5173\u4e8e\u7a0b\u5e8f\u540d\u79f0\u3001\u6267\u884c\u9891\u7387\u7b49\u65b9\u9762\u7684\u6570\u636e\u7edf\u8ba1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL Server \u4e2d\uff0cCOUNT(DISTINCT) \u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u5b83\u7528\u4e8e\u8ba1\u7b97\u67d0\u4e2a\u5b57\u6bb5\u4e2d\u4e0d [&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-965","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/965","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=965"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/965\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/965\/revisions\/966"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}