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

SQL Server ÐÐÁÐת»»Ê¾Àý

SQL ServerµÄÐÐÁÐת»»¹¦Äܷdz£ÊµÓ㬵«ÊÇÓÉÓÚÆäÓï·¨²»ºÃ¶®£¬Ê¹ºÜ¶à³õѧÕß¶¼²»Ô¸ÒâʹÓÃËü¡£ÏÂÃæÎÒ¾ÍÓÃʾÀýµÄÐÎʽ£¬ÖðÒ»Õ¹ÏÖPivotºÍUnPivotµÄ÷ÈÁ¦¡£ÈçÏÂͼ
 
1.´ÓWide Table of Months ת»»µ½ Narrow TableµÄʾÀý
select [Year],[Month],[Sales] from
(
select * from MonthsTable
)p
unpivot
(
[Sales] for [Year] in ([2001],[2002],[2003])
)t
order by [Year]
2.´ÓNarrow Table ת»»µ½ Wide Table of YearsµÄʾÀý
select * from
(
select * from NarrowTable
)p
pivot
(
Sum(Sales) for [Month] in ([Jan],[Feb],[Mar])
)t  
3.´ÓWide Table of Months ת»»µ½ Wide Table of YearsµÄʾÀý
with d as
(
select [Year],[Month],[Sales] from
(
select * from MonthsTable
)p
unpivot
(
[Sales] for [Year] in ([2001],[2002],[2003])
)t
)
select * from
(
select * from d
)p
pivot
(
Sum(Sales) for [Month] in ([Jan],[Feb],[Mar])
)t  
4.´ÓWide Table of Years ת»»µ½ Narrow TableµÄʾÀý
select [Year],[Month],[Sales] from
(
select * from YearTable
)p
unpivot
(
Sales for [Month] in ([Jan],[Feb],[Mar])
)t  
5.´ÓNarrow Table ת»»µ½ Wide Table of MonthµÄʾÀý
select * from
(
select * from NarrowTable
)p
pivot
(
Sum(Sales) for [Year] in ([2001],[2002],[2003])
)t  
6.´ÓWide Table of Years ת»»µ½ Wide Table of MonthµÄʾÀý
with d as
(
select [Year],[Month],[Sales] from
(
select * from YearTable
)p
unpivot
(
Sales for [Month] in ([Jan],[Feb],[Mar])
)t
)
select * from
(
select * from d
)p
pivot
(
Sum(Sales) for [Year] in ([2001],[2002],[2003])
)t
ÈçÐè×ªÔØ£¬Çë×¢Ã÷±¾ÎÄÔ­´´×ÔCSDN TJVictorרÀ¸£ºhttp://blog.csdn.net/tjvictor


Ïà¹ØÎĵµ£º

SQL¿ç·þÎñÆ÷²éѯ

--ÔÚ²éѯ·ÖÎöÆ÷ÖÐ,ÔÚServer·þÎñÆ÷Öд´½¨Á´½Ó·þÎñÆ÷
exec sp_addlinkedserver  'srv_lnk','','SQLOLEDB','·þÎñÆ÷Ãû'
exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'Óû§Ãû','ÃÜÂë'
Go
--ʹÓÃ
select * from srv_lnk.Êý¾Ý¿âÃû.dbo.±íÃû
--¶Ï¿ª
exec sp_dropserver 'srv_lnk','droplogins' ......

SQLÓï¾ä´óÈ«(3)

--Óï ¾ä¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¹¦ ÄÜ
--Êý¾Ý²Ù×÷
SELECT¡¡¡¡¡¡--´ÓÊý¾Ý¿â±íÖмìË÷Êý¾ÝÐкÍÁÐ
INSERT¡¡¡¡¡¡--ÏòÊý¾Ý¿â±íÌí¼ÓÐÂÊý¾ÝÐÐ
DELETE¡¡¡¡¡¡--´ÓÊý¾Ý¿â±íÖÐɾ³ýÊý¾ÝÐÐ
UPDATE¡¡¡¡¡¡--¸üÐÂÊý¾Ý¿â±íÖеÄÊý¾Ý
--Êý¾Ý¶¨Òå
CREATE TABLE¡¡¡¡--´´½¨Ò»¸öÊý¾Ý¿â±í
DROP TABLE¡¡¡¡ --´ÓÊý¾Ý¿âÖÐɾ³ý±í
ALTER ......

SQL PL ¼ò½é

----start
     SQL(Structured Query Language)£¬Ò²¾ÍÊǽṹ»¯²éѯÓïÑÔ£¬Ëü±»Éè¼ÆÓÃÀ´²Ù×÷¼¯ºÏµÄ£¬ÊǷǹý³Ì»¯µÄÓïÑÔ¡£Ëæ×ÅÓ¦ÓóÌÐòµÄ·¢Õ¹£¬ÒµÎñÂß¼­Ô½À´Ô½¸´ÔÓ£¬´«Í³µÄSQLÒѾ­²»ÄÜÂú×ãÈËÃǵÄÒªÇó£¬ÓÚÊÇÈËÃǶÔSQL½øÐÐÁËÀ©Õ¹£¬Ê¹Ëü¾ßÓÐÁ˹ý³Ì»¯µÄÂß¼­£¬¼´£ºSQL PL¡£SQL PLµÄÈ«³ÆÊÇ SQL Procedural Language£ ......

SQL Server2008 Resource Governor¼ò½é


Introduction to the SQL Server 2008 Resource Governor
Database Administration, Performance Tuning | January 4, 2010 | 4:05 pm
This is an excerpt from my free eBook, Brad’s Sure Guide to SQL Server 2008.
I think most of us are familiar with this situation: a SQL Server database is the bac ......

SQLË÷Òý¸ß¼¶Ó¦ÓÃ

--½áºÏsys.indexesºÍsys.index_columns,sys.objects,sys.columns²éѯË÷ÒýËùÊôµÄ±í»òÊÓͼµÄÐÅÏ¢
select
  o.name as ±íÃû,
  i.name as Ë÷ÒýÃû,
  c.name as ÁÐÃû,
  i.type_desc as ÀàÐÍÃèÊö,
  is_primary_key as Ö÷¼üÔ¼Êø,
  is_unique_constraint as Î¨Ò»Ô¼Êø,
  is_disable ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ