ÔÚSQL Server2005ÖÐÓÐÈçÏÂËĸöÅÅÃûº¯Êý
ÅÅÃûº¯ÊýÊÇSQL Server2005мӵŦÄÜ¡£ÔÚSQL Server2005ÖÐÓÐÈçÏÂËĸöÅÅÃûº¯Êý£º
¡¡¡¡1.row_number
¡¡¡¡2.rank
¡¡¡¡3.dense_rank
¡¡¡¡4.ntile¡¡¡¡
¡¡¡¡ÏÂÃæ·Ö±ð½éÉÜÒ»ÏÂÕâËĸöÅÅÃûº¯ÊýµÄ¹¦Äܼ°Ó÷¨¡£ÔÚ½éÉÜ֮ǰ¼ÙÉèÓÐÒ»¸öt_table±í£¬±í½á¹¹Óë±íÖеÄÊý¾ÝÈçͼ1Ëùʾ£º
¡¡¡¡Í¼1
¡¡¡¡ÆäÖÐfield1×ֶεÄÀàÐÍÊÇint£¬field2×ֶεÄÀàÐÍÊÇvarchar
¡¡¡¡Ò»¡¢row_number
¡¡¡¡row_numberº¯ÊýµÄÓÃ;ÊǷdz£¹ã·º£¬Õâ¸öº¯ÊýµÄ¹¦ÄÜÊÇΪ²éѯ³öÀ´µÄÿһÐмǼÉú³ÉÒ»¸öÐòºÅ¡£row_numberº¯ÊýµÄÓ÷¨ÈçÏÂÃæµÄSQLÓï¾äËùʾ£º
select row_number() over(order by field1) as row_number,* fromt_table
¡¡¡¡ÉÏÃæµÄSQLÓï¾äµÄ²éѯ½á¹ûÈçͼ2Ëùʾ¡£
¡¡¡¡Í¼2
¡¡¡¡ÆäÖÐrow_numberÁÐÊÇÓÉrow_numberº¯ÊýÉú³ÉµÄÐòºÅÁС£ÔÚʹÓÃrow_numberº¯ÊýÊÇҪʹÓÃover×Ó¾äÑ¡Ôñ¶ÔijһÁнøÐÐÅÅÐò£¬È»ºó²ÅÄÜÉú³ÉÐòºÅ¡£
¡¡¡¡Êµ¼ÊÉÏ£¬row_numberº¯ÊýÉú³ÉÐòºÅµÄ»ù±¾ÔÀíÊÇÏÈʹÓÃover×Ó¾äÖеÄÅÅÐòÓï¾ä¶Ô¼Ç¼½øÐÐÅÅÐò£¬È»ºó°´×ÅÕâ¸ö˳ÐòÉú³ÉÐòºÅ¡£over×Ó¾äÖеÄorder by×Ó¾äÓëSQLÓï¾äÖеÄorder by×Ó¾äûÓÐÈκιØϵ£¬ÕâÁ½´¦µÄorder by ¿ÉÒÔÍêÈ«²»Í¬£¬ÈçÏÂÃæµÄSQLÓï¾äËùʾ
select row_number() over(order by field2 desc) as row_number,*from t_table order by field1 desc
¡¡¡¡ÉÏÃæµÄSQLÓï¾äµÄ²éѯ½á¹ûÈçͼ3Ëùʾ¡£
¡¡¡¡Í¼3
¡¡¡¡ÎÒÃÇ¿ÉÒÔʹÓÃrow_numberº¯ÊýÀ´ÊµÏÖ²éѯ±íÖÐÖ¸¶¨·¶Î§µÄ¼Ç¼£¬Ò»°ã½«ÆäÓ¦Óõ½WebÓ¦ÓóÌÐòµÄ·ÖÒ³¹¦ÄÜÉÏ¡£ÏÂÃæµÄSQLÓï¾ä¿ÉÒÔ²éѯt_table±íÖеÚ2ÌõºÍµÚ3Ìõ¼Ç¼£º
with t_rowtable
as
(
¡¡¡¡select row_number() over(order by field1) as row_number,*from t_table
)
select * from t_rowtable where row_number>1 and row_number<4 order by field1
¡¡¡¡ÉÏÃæµÄSQLÓï¾äµÄ²éѯ½á¹ûÈçͼ4Ëùʾ¡£
¡¡¡¡Í¼4
¡¡¡¡ÉÏÃæµÄSQLÓï¾äʹÓÃÁËCTE£¬¹ØÓÚCTEµÄ½éÉܽ«¶ÁÕß²ÎÔÄ¡¶SQL Server2005ÔÓ̸£¨1£©£ºÊ¹Óù«Óñí±í´ïʽ£¨CTE£©¼ò»¯Ç¶Ì×SQL¡·¡£
¡¡¡¡ÁíÍâҪעÒâµÄÊÇ£¬Èç¹û½«row_numberº¯ÊýÓÃÓÚ·ÖÒ³´¦Àí£¬over×Ó¾äÖеÄorder by ÓëÅÅÐò¼Ç¼µÄorder by Ó¦Ïàͬ£¬·ñÔòÉú³ÉµÄÐòºÅ¿ÉÄܲ»ÊÇÓÐÐøµÄ¡£
¡¡¡¡µ±È»£¬²»Ê¹ÓÃrow_numberº¯ÊýÒ²¿ÉÒÔʵÏÖ²éѯָ¶¨·¶Î§µÄ¼Ç¼£¬¾ÍÊDZȽÏÂé·³¡£Ò»°ãµÄ·½·¨ÊÇʹÓõߵ¹TopÀ´ÊµÏÖ£¬ÀýÈ磬²éѯt_table±íÖеÚ2ÌõºÍµÚ3Ìõ¼Ç¼£¬¿ÉÒÔÏȲé³öÇ°3Ìõ¼Ç¼£¬È»ºó½«²éѯ³öÀ´µÄÕâÈýÌõ¼Ç¼°´µ¹ÐòÅÅÐò£¬ÔÙÈ¡Ç°2Ìõ¼Ç¼£¬×îºóÔÙ½«²é³öÀ´µÄÕâ2Ìõ¼Ç¼ÔÙ°´µ¹ÐòÅÅÐò£¬¾ÍÊÇ×îÖÕ½
Ïà¹ØÎĵµ£º
SQL ServerÖ®·Ö²¼Ê½ÊÂÎñ
ÊÕ²Ø
--> Title : SQL ServerÖ®·Ö²¼Ê½ÊÂÎñ
--> Author : wufeng4552
--> Date : 2009-11-11
SQL ServerÖ®·Ö²¼Ê½ÊÂÎñ
(Ò»)¸ÅÄî:
·Ö²¼Ê½ÊÂÎñÊÇÉæ¼°À´×ÔÁ½¸ö»ò¶à¸öÔ´µÄ×ÊÔ´µÄÊÂÎñ¡£Microsoft® SQL Serv ......
·ÅÔÚÕâÀﱸÍü,ÀÏÊÇÍü¼ÇÔõôд¡£
mysql> create database book;
mysql> use book;
Database changed
mysql> create table email_message(key_mail_messages INTEGER,
-> date_created VARCHAR(19),
-> date_updated varchar(19),
-> date_email varchar(19),
-> addr_from va ......
±ÈÈçÏÖÔÚÓÐÒ»ÈËÔ±±í £¨±íÃû£ºpeosons£©
ÈôÏ뽫ÐÕÃû¡¢Éí·ÝÖ¤ºÅ¡¢×¡Ö·ÕâÈý¸ö×Ö¶ÎÍêÈ«ÏàͬµÄ¼Ç¼²éѯ³öÀ´
select p1.* from persons p1,persons p2 where p1.id<>p2.id and p1.cardid &nbs ......
дÁËÒ»¸öС³ÌÐò£¬Ê¹ÓÃÁËsql server 2005Êý¾Ý¿â£¬µ±Ê±¸ÃÊý¾Ý¿â·Åµ½ÁËC:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\DataÏÂÃ棬µ«ÊÇÏÖÔÚ¾õµÃ²»·½±ã£¬Ïë°ÑËüתÒƵ½ÎÒÕâ¸öÍøÕ¾´úÂëËùÔÚµÄÄǸöĿ¼£¬±ÈÈçd£º\network\app_dataÏÂÃ棬ÇëÎÊÔõôת¹ýÈ¥£¬Ð»Ð»£¿
»¹ÓÐÒ»¸öÎÊÌ⣬¾ÍÊÇÎÒ¿´C:\Program Files\Microsoft SQL Serve ......
1.²éѯÁ¬½Óµ½Ä³Êý¾Ý¿âµÄÁ¬½ÓÊý
select count(*) as Á¬½ÓÊý from master..sysprocesses where db_name(dbid)='Êý¾Ý¿âÃû' ......