¹ØÓÚSQLÓï¾äCountµÄÒ»µãϸ½Ú
countÓï¾äÖ§³Ö*¡¢ÁÐÃû¡¢³£Á¿¡¢±äÁ¿,²¢ÇÒ¿ÉÒÔÓÃdistinct¹Ø¼ü×ÖÐÞÊΣ¬ ²¢ÇÒcount(ÁÐÃû)²»»áÀÛ¼ÆnullµÄ¼Ç¼¡£ÏÂÃæËæ±ãÓÃһЩÀý×Óʾ·¶Ò»ÏÂcountµÄ¹æÔò£º±ÈÈç¶ÔÈçϱí×öͳ¼Æ£¬ËùÓÐÁÐÕâÀï¶¼ÓÃsql_variantÀàÐÍÀ´±íʾ¡£
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 (* ) --×ÜÊý
, count (nullif (1 , 1 )) --ÓÀÔ¶·µ»Ø0
, count (a ) --aÊýÁ¿
, count (b) --bÊýÁ¿
, count (distinct a ) --a²»Öظ´ÊýÁ¿
, count (isnull (b, c )) --b»òÕßc²»ÎªnullÊýÁ¿
, count (Coalesce (a , b, c )) --a»òÕßb»òÕßc²»ÎªnullÊýÁ¿
, count (nullif (a , b)) --a²»µÈÓÚbµÄÊýÁ¿
, count (nullif (isnumeric (cast (a as varchar (38 ))), 0 ))--aÊÇÊý×ÖµÄÊýÁ¿
from t_test
Ïà¹ØÎĵµ£º
Ò»£ºSelectÓï¾ä£º
select ×Ö¶ÎÃû from ±íÃû where Ìõ¼þ order by ÅÅÐò
see:select distinct ´Ó¶à¸öÏàͬ×Ö¶ÎÖÐץΨһֵ
see:²éÕÒld_det_andy±íÖÐÓÐÊý¾Ýµ«ld_det_temp±íÖÐûÊý¾ÝµÄÊý¾Ý
select * from ld_det_a ......
ÓÐʱÔÚ×ö³ÌÐòʱ£¬²âÊÔʱµÄÊý¾Ý£¬ÒªÄÃÏñÓû§ÑÝʾ£¬Êý¾Ý¿âµÄ¸½¼ÓÊÇ×îÖ±½ÓµÄ°ì·¨£¬µ«Èô´ÓSQL Server 2005תÏò2000µ¼³öÂé·³£¬ÄÚÈÝÒ²¶à£¬Éú³ÉSQLÓï¾äÊÇ×îºÃµÄ°ì·¨£¬Ò²ÊÇÔÚÍøÉÏÕҵŤ¾ß,mssql2µ¼³ö·Ç³£·½±ã£¬µ£ÐÄÎÒÒÔºóÄѵÃÕÒËùÒÔ·ÅÔÚÕâÀÒÔ±¸ºóÓᣠ......
select *from student
select student_id from student
select student_id ,student_name from student
select student_id student_name from student ½«student_name ×÷Ϊstudent_idµÄ±ðÃû´¦Àí
È磺 select student_id a from student
select a=student_id from student
´Óstudent±íÖзֱð¼ ......
/*
Ô±í£º
thid other
a 1
a 1
b 0
b 0
......
----start
ͨ³£SQL PLÖ»ÄÜʹÓÃÔÚ´æ´¢¹ý³Ì¡¢´¥·¢Æ÷¡¢Óû§×Ô¶¨Ò庯ÊýÖУ¬µ«ÊÇÓÐÒ»²¿·ÖSQL PLÒ²¿ÉÒÔÖ±½ÓÔÚÃüÁîÐÐ±à¼Æ÷»ò½Å±¾ÖÐʹÓã¬ËüÃÇÊÇ£º
DECLARE <variable>
SET
CASE
FOR
GET DIAGNOSTICS
GOTO
IF
RETURN
SIGNAL
WHILE
ITERATE
LEAVE
ÒÔÏÂSQL PL²»ÄÜÖ±½ÓÔÚÃüÁîÐÐ±à¼Æ÷»ò ......