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 2005 (MSSQLSERVER) ·þÎñ²»ÄÜÆô¶¯
ÔÒò:VIAÐÒ锸øÆôÓÃÁË£¬Í£ÓÓVIAÐÒé”ÎÊÌâ½â¾ö¡£
"VIAÐÒé"Í£Ó÷½·¨£º¿ªÊ¼->³ÌÐò->Microsoft SQL Server 2005->ÅäÖù¤¾ß->SQL Server Configuration Manager ->´ò¿ªºóÕÒµ½"SQL Server 2005 ÍøÂçÅäÖÃ"->MSSQLSERVER ÊôÐÔµÄÐÒé &nb ......
1.Union
UNION Ö¸ÁîµÄÄ¿µÄÊǽ«Á½¸ö SQL Óï¾äµÄ½á¹ûºÏ²¢ÆðÀ´¡£´ÓÕâ¸ö½Ç¶ÈÀ´¿´£¬ UNION ¸ú JOIN ÓÐЩÐíÀàËÆ£¬ÒòΪÕâÁ½¸öÖ¸Áî¶¼¿ÉÒÔÓɶà¸ö±í¸ñÖÐߢȡ×ÊÁÏ¡£ UNION µÄÒ»¸öÏÞÖÆÊÇÁ½¸ö SQL Óï¾äËù²úÉúµÄÀ¸Î»ÐèÒªÊÇͬÑùµÄ×ÊÁÏÖÖÀà¡£ÁíÍ⣬µ±ÎÒÃÇÓà UNIONÕâ¸öÖ¸Áîʱ£¬ÎÒÃÇÖ»»á¿´µ½²»Í¬µÄ×ÊÁÏÖµ (ÀàËÆ SELECT DISTINCT)¡£
UNION µ ......
select ÐÕÃû,סַ,ÆÚ³õÓà¶î=isnull(ÆÚ³õÔö¼Ó,0)-isnull(ÆÚ³õ¼õÉÙ,0),±¾ÆÚÔö¼Ó,±¾ÆÚ¼õÉÙ,
±¾ÆÚ½áÓà=(isnull(ÆÚ³õÔö¼Ó,0)-isnull(ÆÚ³õ¼õÉÙ,0)+isnull(±¾ÆÚÔö¼Ó,0)-isnull(±¾ÆÚ¼õÉÙ,0)) from (
select ÐÕÃû,סַ,
ÆÚ³õÔö¼Ó=(select ÆÚ³õÔö¼Ó=sum(Ôö¼Ó»ý·Ö) from b where ·¢ÉúÈÕÆÚ<'2006-5-1' and ¿¨ºÅ=a.¿¨ºÅ),
ÆÚ³õ¼õÉ ......
1.ʹÓÃCTE¼ò»¯Ç¶Ì×sql
ÏÈ¿´ÏÂÃæÒ»¸öǶÌ׵IJéѯÓï¾ä£º
select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where  ......
×î½ü×öÏîÄ¿£¬ÐèÒªÔ¶³Ì·ÃÎÊÊý¾Ý¿â£¬×Ô¼ºÔÚ²éÁËЩ×ÊÁÏ£¬×ܽáÁËһϣ¬Ï£Íû¶Ô´ó¼ÒÓаïÖú£º
×÷Õߣºshinehoo
Ò»¡¢ÅäÖÃSQL Server 2005
1£©°²×°SQL Server 2005¿ª·¢°æ£»
2£©¿ªÊ¼->³ÌÐò->Microsoft SQL Server 2005->SQL
Server 2005ÍâΧӦÓÃÅäÖÃÆ÷£¬ÔÚ´ò¿ªµÄ½çÃæµ¥»÷“·þÎñµÄÁ¬½ÓµÄÍâΧӦÓÃÅäÖÃÆ÷”£¬ÔÚ´ò ......