SQL Server中不同count()和count(distinct)之间的区别 [英] Difference between distinct count() and count(distinct) in Sql Server

查看:495
本文介绍了SQL Server中不同count()和count(distinct)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从< table_name>
中选择count(distinct< colname>)
从< table_name>
中选择不同的count(< colname>)

两者有什么区别?


当您以这种方式编写时,请plz说明

select count(distinct <colname>) from <table_name>

select distinct count(<colname>) from <table_name>


What is the difference between these two ?


plz explain

推荐答案

1)..
1) when u write this way..
select   count(distinct ACODE) from dbo.Table_1


那么它会计算不同的值,例如我的表(dbo.table_1)的ACODE列具有某些值


then it counts for different values like my table(dbo.table_1) having ACODE column which has certain values

ACODE
2010
2010
2009
2011
2011
2009
2010
2011



现在,如果我执行第一个查询将得到ans = 3,则bcoz仅计算不同的值.

2)现在,



now if i execute first query will get ans = 3, bcoz it counts only distinct values.

2)now,

select  distinct count(ACODE) from dbo.Table_1

如果您执行此操作,那么您将获得答案= 8

bcoz,它是2010年的3倍,2011年的3倍和2009年的2倍

此计数将计入所有记录并给出输出,而不会计入不同的值.

3)如果您想对它进行更深入的分析,则可以通过取消此查询来进行尝试..

if u execute this then u will get answer = 8

bcoz, it has 3 times -2010, 3 times - 2011 , and 2 times - 2009

this count for all the records and give output, and does not give count for distinct value.

3)if u want to analyse it more deeply then try it by exicuting this query..

select   distinct count(ACODE),ACODE from dbo.Table_1 group by ACODE




希望得到帮助.




hope this help..


阅读以下内容:
计数(Transact-SQL) [
Read the following :
COUNT (Transact-SQL)[^]


这篇关于SQL Server中不同count()和count(distinct)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆