解析關(guān)于SQL語(yǔ)句Count的一點(diǎn)細(xì)節(jié)
更新時(shí)間:2013年06月26日 17:21:21 作者:
本篇文章是對(duì)關(guān)于SQL語(yǔ)句Count的一點(diǎn)細(xì)節(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
count語(yǔ)句支持*、列名、常量、變量,并且可以用distinct關(guān)鍵字修飾, 并且count(列名)不會(huì)累計(jì)null的記錄。下面隨便用一些例子示范一下count的規(guī)則:比如對(duì)如下表做統(tǒng)計(jì),所有列這里都用sql_variant類型來(lái)表示。

if (object_id ('t_test' )> 0 )
drop table t_test
go
create table t_test (a sql_variant , b sql_variant , c sql_variant )
insert into t_test select 1 , 1 , 'a'
insert into t_test select 1 , getdate (), null
insert into t_test select 'a' , null , 1
insert into t_test select 3 , null , null
insert into t_test select null , null , null
go
select * from t_test
go
select
count (* ) --總數(shù)
, count (nullif (1 , 1 )) --永遠(yuǎn)返回0
, count (a ) --a數(shù)量
, count (b) --b數(shù)量
, count (distinct a ) --a不重復(fù)數(shù)量
, count (isnull (b, c )) --b或者c不為null數(shù)量
, count (Coalesce (a , b, c )) --a或者b或者c不為null數(shù)量
, count (nullif (a , b)) --a不等于b的數(shù)量
, count (nullif (isnumeric (cast (a as varchar (38 ))), 0 ))--a是數(shù)字的數(shù)量
from t_test

復(fù)制代碼 代碼如下:
if (object_id ('t_test' )> 0 )
drop table t_test
go
create table t_test (a sql_variant , b sql_variant , c sql_variant )
insert into t_test select 1 , 1 , 'a'
insert into t_test select 1 , getdate (), null
insert into t_test select 'a' , null , 1
insert into t_test select 3 , null , null
insert into t_test select null , null , null
go
select * from t_test
go
select
count (* ) --總數(shù)
, count (nullif (1 , 1 )) --永遠(yuǎn)返回0
, count (a ) --a數(shù)量
, count (b) --b數(shù)量
, count (distinct a ) --a不重復(fù)數(shù)量
, count (isnull (b, c )) --b或者c不為null數(shù)量
, count (Coalesce (a , b, c )) --a或者b或者c不為null數(shù)量
, count (nullif (a , b)) --a不等于b的數(shù)量
, count (nullif (isnumeric (cast (a as varchar (38 ))), 0 ))--a是數(shù)字的數(shù)量
from t_test
您可能感興趣的文章:
相關(guān)文章
SQL Server 遠(yuǎn)程更新目標(biāo)表數(shù)據(jù)的存儲(chǔ)過(guò)程
這篇文章主要介紹了SQL Server 遠(yuǎn)程更新目標(biāo)表數(shù)據(jù)的存儲(chǔ)過(guò)程,適用于更新列名一致,主鍵為Int類型,具體實(shí)例代碼大家參考下本文2018-05-05SQL Server誤區(qū)30日談 第23天 有關(guān)鎖升級(jí)的誤區(qū)
在SQL Server 2005和之前的版本,頁(yè)鎖會(huì)直接升級(jí)到表鎖。在SQL Server 2005或SQL Server 2008,你可以通過(guò)如下跟蹤標(biāo)志改變鎖升級(jí)的行為2013-01-01sql不常用函數(shù)總結(jié)以及事務(wù),增加,刪除觸發(fā)器
sql不常用函數(shù)總結(jié)以及事務(wù),增加,刪除觸發(fā)器,需要的朋友可以參考下2012-06-06將Sql Server對(duì)象的當(dāng)前擁有者更改成目標(biāo)擁有者
將Sql Server對(duì)象的當(dāng)前擁有者更改成目標(biāo)擁有者...2006-10-10通過(guò)sql存儲(chǔ)過(guò)程發(fā)送郵件的方法
如何配置SQL Server利用Internet 郵件服務(wù)器發(fā)送郵件? 請(qǐng)看下面:2013-03-03SqlServer 數(shù)據(jù)庫(kù) 三大 范式
本文將基于三大范式原則,結(jié)合具體的實(shí)例做簡(jiǎn)要分析,對(duì)SqlServer 數(shù)據(jù)庫(kù) 三大 范式相關(guān)知識(shí)感興趣的朋友一起看看吧2019-11-11