¹ØÓÚ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
Ïà¹ØÎĵµ£º
MS SQL Server2000 Êý¾ÝÔ´ÅäÖÃ
£¨×¢£ºsqljdbc.jarÏÂÔØ²»µ½µÄ»°£¬ÕÒÎÒË÷È¡pengqinghui110@126.com)
1¡¢ÔÚÏîÄ¿µÄWebRootϵÄMETA-INFÖÐн¨context.xmlÎļþ¡£ÄÚÈÝÈçÏ£º
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/sqlserver"
&nbs ......
1 ÓÃUNIONÌæ»»OR (ÊÊÓÃÓÚË÷ÒýÁÐ)
ͨ³£Çé¿öÏÂ, ÓÃUNIONÌæ»»WHERE×Ó¾äÖеÄOR½«»áÆðµ½½ÏºÃµÄЧ¹û. ¶ÔË÷ÒýÁÐʹÓÃOR½«Ôì³ÉÈ«±íɨÃè. ×¢Òâ, ÒÔÉϹæÔòÖ»Õë¶Ô¶à¸öË÷ÒýÁÐÓÐЧ.
Èç¹ûÓÐcolumnûÓб»Ë÷Òý, ²éѯЧÂÊ¿ÉÄÜ»áÒòΪÄãûÓÐÑ¡ÔñOR¶ø½µµÍ. ÔÚÏÂÃæµÄÀý×ÓÖÐ, LOC_ID ºÍREGIONÉ϶¼½¨ÓÐË÷Òý.
¸ßЧ: SELECT LOC_ID , LOC_DESC , ......
Ö÷¼ü:
Äܹ»Î¨Ò»±íʾÊý¾Ý±íÖеÄÿ¸ö¼Ç¼µÄ¡¾×ֶΡ¿»òÕß¡¾×ֶΡ¿µÄ×éºÏ¾Í³ÆÎªÖ÷Âë(Ö÷¼ü)¡£ Ò»¸öÖ÷¼üÊÇΨһʶ±ðÒ»¸ö±íµÄÿһ¼Ç¼£¬µ«ÕâÖ»ÊÇÆä×÷ÓõÄÒ»²¿·Ö£¬Ö÷¼üµÄÖ÷Òª×÷ÓÃÊǽ«¼Ç¼ºÍ´æ·ÅÔÚÆäËû±íÖеÄÊý¾Ý½øÐйØÁª¡£ÔÚÕâÒ»µãÉÏ£¬Ö÷¼üÊDz»Í¬±íÖи÷¼Ç¼֮¼äµÄ¼òµ¥Ö¸Õë¡£Ö÷¼üÔ¼Êø¾ÍÊÇÈ·¶¨±íÖеÄÿһÌõ¼Ç¼¡£Ö÷¼ü²»ÄÜÊÇ¿ÕÖµ¡£Î¨Ò ......
----start
ͨ³£SQL PLÖ»ÄÜʹÓÃÔÚ´æ´¢¹ý³Ì¡¢´¥·¢Æ÷¡¢Óû§×Ô¶¨Ò庯ÊýÖУ¬µ«ÊÇÓÐÒ»²¿·ÖSQL PLÒ²¿ÉÒÔÖ±½ÓÔÚÃüÁîÐÐ±à¼Æ÷»ò½Å±¾ÖÐʹÓã¬ËüÃÇÊÇ£º
DECLARE <variable>
SET
CASE
FOR
GET DIAGNOSTICS
GOTO
IF
RETURN
SIGNAL
WHILE
ITERATE
LEAVE
ÒÔÏÂSQL PL²»ÄÜÖ±½ÓÔÚÃüÁîÐÐ±à¼Æ÷»ò ......