Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQL Server2005 applyµÄһЩÔËÓÃ

Àý×Ó£ºÕÒ³öÿ¸ö¸èÊÖÏúÁ¿×î¸ßµÄ3Ê׸è
create table singer_info
(
id int primary key identity(1,1),
name varchar(20),
song varchar(20),
amount int
)
 
insert into singer_info values('jay','aaa',1000)
insert into singer_info values('jay','bbb',2000)
insert into singer_info values('jay','ccc',3000)
insert into singer_info values('jay','ddd',4000)
insert into singer_info values('jay','eee',5000)
insert into singer_info values('jay1','fff',1000)
insert into singer_info values('jay1','ggg',2000)
insert into singer_info values('jay1','hhh',3000)
insert into singer_info values('jay1','iii',4000)
insert into singer_info values('jay2','jjj',1000)
insert into singer_info values('jay2','kkk',2000)
insert into singer_info values('jay2','lll',3000)
insert into singer_info values('jay2','mmm',4000)
insert into singer_info values('jay3','nnn',1000)
 
(1)ʹÓÃfunction apply
create function gettop
(@name varchar(20))
returns table
as
return (select top(3)id,name,song,amount
from singer_info
where name = @name
order by amount desc)
 
select distinct b.id,b.name,b.song,b.amount
from singer_info a
cross apply
gettop(a.name)as b
 
(2)ʹÓÃapply
select distinct c.id,c.name,c.song,c.amount
from singer_info a
cross apply
(select top(3)id,name,song,amount
from singer_info b
where b.name = a.name
order by amount desc) as c
order by c.name asc,c.amount desc
 
(3)ʹÓÃover partition by
select * from
(select a.id,a.name,a.song,a.amount,
row_number() over(partition by a.name order by a.name,a.amount desc) rn
from singer_info a)b
where b.rn<=3


Ïà¹ØÎĵµ£º

SQL Server Linked Server ·ÃÎÊÔ¶³ÌÊý¾Ý¿â

ÔÚʹÓÃSQL ServerÊý¾Ý¿âʱ, Èç¹ûÐèÒª·ÃÎÊÔ¶³ÌµÄÁíÍâÒ»¸öÊý¾Ý¿â, ÄÇô¿ÉÒÔʹÓÃLinked ServerÀ´ÊµÏÖÕâ¸ö¹¦ÄÜ, ºÜ·½±ã. ±¾ÎÄ×ܽáÁË×Ô¼ºÔÚʹÓÃLinked Server¹¦ÄÜʱÓöµ½µÄ¼¸¸öÎÊÌâ, Èç¹ûÉÔ²»×¢Òâ,ºÜ¿ÉÄܳöÏÖ¸÷ÖÖ´íÎó.
1)´´½¨ÁËÒ»¸öеÄLinked Serverºó, ÐèÒªÊ×ÏÈÉèÖÃlogin ÕË»§, ÓÒ¼ü->ÊôÐÔ->Security, Èçͼ
......

¾­µäSQLÓï¾ä´óÈ«

¾­µäSQLÓï¾ä´óÈ«
ÏÂÁÐÓï¾ä²¿·ÖÊÇMssqlÓï¾ä£¬²»¿ÉÒÔÔÚaccessÖÐʹÓá£
¡¡¡¡SQL·ÖÀࣺ
¡¡¡¡DDL—Êý¾Ý¶¨ÒåÓïÑÔ(CREATE£¬ALTER£¬DROP£¬DECLARE)
¡¡¡¡DML—Êý¾Ý²Ù×ÝÓïÑÔ(SELECT£¬DELETE£¬UPDATE£¬INSERT)
¡¡¡¡DCL—Êý¾Ý¿ØÖÆÓïÑÔ(GRANT£¬REVOKE£¬COMMIT£¬ROLLBACK)
¡¡¡¡Ê×ÏÈ,¼òÒª½éÉÜ»ù´¡Óï¾ä£º
¡¡¡¡1¡¢Ë ......

SQL ServerÖÐpivot and unpivotµÄÓ÷¨ £¨ÐÐÁл¥×ª£©

 .PivotµÄÓ÷¨Ìå»á:
Óï¾ä·¶Àý:
select PN,[2006/5/30] as [20060530],[2006/6/2] as [20060602]
from consumptiondata a
Pivot (sum(a.M_qty) FOR a.M_date in ([2006/5/30],[2006/6/2])) as PVT 
order by PN
 
Table½á¹¹ Consumptiondata (PN,M_Date,M_qty)
order by PN¿ÉÒª¿É²»Òª,²¢²»ÖØ ......

SQL ServerÖд洢¹ý³Ì±ÈÖ±½ÓÔËÐÐSQLÓï¾äÂýµÄÔ­Òò

ÔںܶàµÄ×ÊÁÏÖж¼ÃèÊö˵SQLSERVERµÄ´æ´¢¹ý³Ì½ÏÆÕͨµÄSQLÓï¾äÓÐÒÔÏÂÓŵ㣺
1.       ´æ´¢¹ý³ÌÖ»ÔÚ´´Ôìʱ½øÐбàÒë¼´¿É£¬ÒÔºóÿ´ÎÖ´Ðд洢¹ý³Ì¶¼²»ÐèÔÙÖØÐ±àÒ룬¶øÎÒÃÇͨ³£Ê¹ÓõÄSQLÓï¾äÿִÐÐÒ»´Î¾Í±àÒëÒ»´Î,ËùÒÔʹÓô洢¹ý³Ì¿ÉÌá¸ßÊý¾Ý¿âÖ´ÐÐËÙ¶È¡£
2.     &nbs ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ