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

SQL server2005ÖÐÓÃpivotʵÏÖÐÐÁÐת»»

 --> --> (Roy)Éú³É²âÊÔÊý¾Ý
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'ÕÅÈý',N'ÓïÎÄ',78 union all
select N'ÕÅÈý',N'Êýѧ',87 union all
select N'ÕÅÈý',N'Ó¢Óï',82 union all
select N'ÕÅÈý',N'ÎïÀí',90 union all
select N'ÀîËÄ',N'ÓïÎÄ',65 union all
select N'ÀîËÄ',N'Êýѧ',77 union all
select N'ÀîËÄ',N'Ó¢Óï',65 union all
select N'ÀîËÄ',N'ÎïÀí',85
Go
¶¯Ì¬:
declare @s nvarchar(4000)
Select  @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')
Éú³É¾²Ì¬:
select *
from
Class
pivot
(max([Score]) for [Course] in([Êýѧ],[ÎïÀí],[Ó¢Óï],[ÓïÎÄ]))b
Éú³É¸ñʽ:
/*
Student Êýѧ          ÎïÀí          Ó¢Óï          ÓïÎÄ
------- ----------- ----------- ----------- -----------
ÀîËÄ      77          85          65          65
ÕÅÈý      87          90          82          78
(2 ÐÐÊÜÓ°Ïì)
*/
--2000·½·¨:
¶¯Ì¬:
declare @s nvarchar(4000)
set @s=''
Select  @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
Éú³É¾²Ì¬:
select
[Student],
[Êýѧ]=max(case when [Course]='Êýѧ' then [Score] else 0 end),
[ÎïÀí]=max(case when [Course]='ÎïÀí' then [Score] else 0 end),
[Ó¢Óï]=max(case when [Course]='Ó¢Óï' then [Score] else 0 end),
[ÓïÎÄ]=max(case whe


Ïà¹ØÎĵµ£º

¸ßÈËÇóÖú£¬SQLÎÊÌ⣡

ԭʼÊý¾Ý
 
  
TERMINAL_ID
MAXDATE
TERMINAL_ID
OCCUR_DATE_TIME
TROUBLE_CD
  
1
12345
20100401102754
12345
20100401102754
210
  
2
12345
20100401102754
12345
20100401102754
211
  
3
12345
20100401102754
12345
20100401102754
?09
......

ʹÓÃSQL²Ù×÷XML¼òµ¥Ê¾Àý

declare @xml xml
set @xml = '<root/>'
select @xml
declare @value varchar(10)
set @value = 'val1'
set @xml.modify('insert <item value="{sql:variable("@value")}" /> into (/root)[1]')
select @xml
set @value = 'val2'
set @xml.modify('replace value of (/root/item/@value)[1] with "val2 ......

Sql server With

with HostDevice as (----ÉÌ»§Ö÷»ú
select TerminalID ,Deviceid hostDeviceid ,Device.ModelID,Device.SN HostSN,Device.MerchantID,Device.InstallAddress,Device.SoftVersion
from Device
join model HostM on Device.ModelID=HostM.ModelID and HostM.Category in(0,3,4,5,6,8)
--where TerminalID is not null
) ......

SQL Server 2005ÖеÄT SQLÔöÇ¿(3) TOP OUTPUT

TOP ÔöÇ¿¹¦ÄÜ
1¡¢TOP ÔöÇ¿¡£¿ÉÒÔÖ¸¶¨Ò»¸öÊý×Ö±í´ïʽ£¬ÒÔ·µ»ØҪͨ¹ý²éѯӰÏìµÄÐÐÊý»ò°Ù·Ö±È£¬»¹¿ÉÒÔ¸ù¾ÝÇé¿öʹÓñäÁ¿»ò×Ó²éѯ¡£
¿ÉÒÔÔÚDELETE¡¢UPDATEºÍINSERT²éѯÖÐʹÓÃTOPÑ¡Ïî¡£
2¡¢¸üºÃµØÌæ»»SET ROWCOUNTÑ¡Ïʹ֮¸üΪÓÐЧ¡£
OUTPUT
1¡¢SQL Server 2005ÒýÈëÒ»¸öеÄOUTPUT×Ӿ䣬ÒÔʹÄú¿ÉÒÔ³åÐÞ¸ÄÓï¾ä(INSERT¡ ......

SQL Server 2005ÖеÄT SQLÔöÇ¿(7) ͨÓñí±í´ïʽCTE

ͨÓñí±í´ïʽ Common Table Expressions
ͨÓñí±í´ïʽ£¨CTE£©ÊÇÒ»¸ö¿ÉÒÔÓɶ¨ÒåÓï¾äÒýÓõÄÁÙʱ±íÃüÃûµÄ½á¹û¼¯¡£ÔÚËûÃǵļòµ¥ÐÎʽÖУ¬Äú¿ÉÒÔ½«CTEÊÓΪÀàËÆÓÚÊÓͼºÍÅÉÉú±í»ìºÏ¹¦ÄܵĸĽø°æ±¾¡£ÔÚ²éѯµÄfrom×Ó¾äÖÐÒýÓÃCTEµÄ·½Ê½ÀàËÆÓÚÒýÓÃÅÉÉú±íºÍÊÓͼµÄ·½Ê½¡£Ö»Ð붨ÒåCTEÒ»´Î£¬¼´¿ÉÔÚ²éѯÖжà´ÎÒýÓÃËü¡£ÔÚCTEµÄ¶¨ÒåÖУ¬¿ÉÒÔÒ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ