SQL¸ß¼¶Ó¦ÓÃ
Garin Zhang
×·¼Ó£ºrow_number, rank, dese_rank, ntile
1. row_number: Ϊ²éѯ³öÀ´µÄÿһÐмǼÉú³ÉÒ»¸öÐòºÅ¡£
SELECT row_number() OVER(ORDER BY field) AS row_n
from tablename;
·ÖÒ³²éѯ£º
with t_towtable
as (select row_number over(order by field1) as row_number from tb)
select * from t_rowtable where row_number > 1 and row_number > 4 order by field1;
ROW_NUMBER() OVER([<partition_by_caluse>] <order_by_clause>);
2. rank: ·µ»Ø½á¹û¼¯µÄ·ÖÇøÄÚÿÐеÄÅÅÃû£º
RANK() OVER([<partition_by_caluse_°´ÕÕij¸ö×ֶηÖÇø>]<order_by_clause_ÅÅÐò>);
Examples£º
WITH CustomerSum AS
(SELECT CustomerID, SUM(totalDue) AS total
from sales GROUP BY CustomerID)
SELECT * RANK() OVER (ORDER BY total DESC) AS RANK
from CustomerSum
3. dense_rank: ˳ÐòÅÅÐò£¨Éú³ÉµÄÐòºÅÊÇÁ¬ÐøµÄ£©¡£
4. ntile£ºÈ¡Ç°¶àÉÙ¸öÃû´ÎµÄÅÅÃûµÄÐС£
SELECT SalesID, NTILE(1000) OVER(ORDER BY CustomerID) AS NTile
from Sales
Í°ÊýµÄ¼ÆË㣺
if(¼Ç¼Êý%Í°Êý == 0)
ÿͰ¼Ç¼Êý¶¼Îª¼ÇÊý×ÜÊý/Í°Êý
else
recordCount1 = total/tongNum + 1;
int n = 1;
m = recordCont1*n;
while((total - m) % (tongNum - n)) != 0)
{
n++;
m = recordCount1 * n;
}
recordCount2 = (total - m) / (tongNum - n);
½«Ç°n¸öÍ°¼Ç¼ÉèÖÃΪrecordCount1
½«n+1¸öµ½ºóÃæËùÓÐÍ°¼ÇΪrecordCount2
Ïà¹ØÎĵµ£º
SQL ServertimestampÊý¾ÝÀàÐÍÓëʱ¼äºÍÈÕÆÚÎ޹ء£SQL ServertimestampÊǶþ½øÖÆÊý×Ö£¬Ëü±íÃ÷Êý¾Ý¿âÖÐÊý¾ÝÐ޸ķ¢ÉúµÄÏà¶Ô˳Ðò¡£ÊµÏÖtimestampÊý¾ÝÀàÐÍ×î³õÊÇΪÁËÖ§³Ö SQL Server »Ö¸´Ëã·¨¡£Ã¿´ÎÐÞ¸Äҳʱ£¬¶¼»áʹÓõ±Ç°µÄ @@DBTS Öµ¶ÔÆä×öÒ»´Î±ê¼Ç£¬È»ºó @@DBTS ¼Ó1¡£ÕâÑù×ö×ãÒÔ°ïÖú»Ö¸´¹ý³ÌÈ·¶¨Ò³Ð޸ĵÄÏà¶Ô´ÎÐò£¬µ«ÊÇtimest ......
Microsoft SQL Server 2008 ¶Ô T-SQL ÓïÑÔ½øÐÐÁ˽øÒ»²½ÔöÇ¿¡£ÎªÁËÈÿª·¢ÈËÔ±¾¡¿ìÁ˽âÕâЩ±ä»¯£¬ÎÒÃÇÕë¶Ô 2007 Äê 6 Ô CTP °æ±¾µÄ SQL Server 2008 ÖÐµÄ T-SQL ÓïÑÔµÄÐÂÔö¹¦ÄܽøÐÐÁË·ÖÎöºÍ³¢ÊÔ¡£±¾ÎÄÃèÊö×Ô SQL Server 2008 CTP1 ÒÔÀ´ÕâЩÓïÑÔÔöÇ¿ºÍ±ä»¯¡£
¡¡¡¡±¾ÎÄ°üº¬ÈçÏÂÄÚÈÝ£º
¡¡¡¡· T-SQL Ðй¹ÔìÆ÷
¡¡¡¡&mid ......
Ëø»úÖÆ
NOLOCKºÍREADPASTµÄÇø±ð¡£
1. ¿ªÆôÒ»¸öÊÂÎñÖ´ÐвåÈëÊý¾ÝµÄ²Ù×÷¡£
BEGIN TRAN t
INSERT INTO Customer
SELECT 'a','a'
2. Ö´ÐÐÒ»Ìõ²éѯÓï¾ä¡£
SELECT * from Customer WITH (NOLOCK)
½á¹ûÖÐÏÔʾ"a"ºÍ"a"¡£µ±1ÖÐÊÂÎñ»Ø¹öºó£¬ÄÇôa½«³ÉΪÔàÊý¾Ý¡£(×¢:1ÖеÄÊÂÎñδÌá½») ¡£NOLOCK±íÃ÷ûÓжÔÊý¾Ý±íÌí¼Ó¹²Ï ......
Student(S#,Sname,Sage,Ssex) ѧÉú±í
Course(C#,Cname,T#) ¿Î³Ì±í
SC(S#,C#,score) ³É¼¨±í
Teacher(T#,Tname) ½Ìʦ±í
ÎÊÌ⣺
1¡¢²éѯ“001”¿Î³Ì±È“002”¿Î³Ì³É¼¨¸ßµÄËùÓÐѧÉúµÄѧºÅ£»
select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
fr ......