ÒÀ¾Ýcsdn¸ßÊÖдµÄ×Ô¼ºÁ·Ï°Ò»Ï·½±ãÒÔºó²éÕÒ
--Creator:Gongl
--Date:2009-1-8
--sql server 2000
--ѧϰÐÐתÁУ¬ÎªÁ˽øÒ»²½Á˽⶯̬sqlÆ´½Ó£¨µ¥Ë«ÈýÒýºÅ£©
--¼¸ÖÖÀàÐÍ
--Numeric(10,2) Ö¸×Ö¶ÎÊÇÊý×ÖÐÍ,³¤¶ÈΪ10 СÊýΪÁ½Î»
--varcharºÍnvarcharµÄÇø±ð
--1.´Ó´æ´¢·½Ê½ÉÏ£¬nvarcharÊǰ´×Ö·û´æ´¢µÄ£¬¶ø varcharÊǰ´×Ö½Ú´æ´¢µÄ£»
--2.´Ó´æ´¢Á¿ÉÏ¿¼ÂÇ£¬ varchar±È½Ï½ÚÊ¡¿Õ¼ä£¬ÒòΪ´æ´¢´óСΪ×Ö½ÚµÄʵ¼Ê³¤¶È£¬¶ø nvarcharÊÇË«×Ö½Ú´æ´¢£»
--3.ÔÚʹÓÃÉÏ£¬Èç¹û´æ´¢ÄÚÈݶ¼ÊÇÓ¢ÎÄ×Ö·û¶øÃ»Óкº×ÖµÈÆäËûÓïÑÔ·ûºÅ£¬½¨ÒéʹÓÃvarchar£»º¬Óкº×ÖµÄʹÓÃnvarchar£¬ÒòΪnvarcharÊÇʹÓÃUnicode±àÂ룬¼´Í³Ò»µÄ×Ö·û±àÂë±ê×¼£¬»á¼õÉÙÂÒÂëµÄ³öÏÖ¼¸ÂÊ£»
----ÐÐתÁÐ
--´´½¨²âÊÔÊý¾Ý
if object_id('idl') is not null drop table idl
create table idl(name varchar(10),subject nvarchar(10),score numeric(4,1))
insert into idl
select 'anny','Êýѧ',95.5 union all
select 'anny','ÓïÎÄ',90 union all
select 'anny','Ó¢Óï',99 union all
select 'anny','asp.net',100 union all
select 'anny','sqlserver',100 union all
select 'jenny','Êýѧ',94.5 union all
select 'jenny','ÓïÎÄ',59.5 union all
select 'jenny','asp.net',100
--¾²Ì¬·½·¨1
select [name],
max(case subject when 'Êýѧ' then score else 0 end) [Êýѧ],
max(case subject when 'ÓïÎÄ' then score else 0 end) [ÓïÎÄ],
max(case subject when 'Ó¢Óï' then score else 0 end) [Ó¢Óï],
max(case subject when 'asp.net' then score else 0 end) [asp.net],
max(case subject when 'sqlserver' then score else 0 end) [sqlserver]
from idl
group by [name]
--¾²Ì¬·½·¨2
select [name],
max(isnull((case subject when 'Êýѧ' then score end),0)) [Êýѧ],
max(isnull((case subject when 'ÓïÎÄ' then score end),0)) [ÓïÎÄ],
max(isnull((case subject when 'Ó¢Óï' then score end),0)) [Ó¢Óï],
isnull(max(case subject when 'asp.net' then score end),0) [asp.net],
isnull(max(case subject when 'sqlserver' then score end),0) [sqlserver]
from idl
group by [name]
--¶¯Ì¬·½·¨
--Èý¸öµ¥ÒýºÅ£¬ÆäÖÐÓÐÁ½¸öµ¥ÒýºÅÊÇתÒå×Ö·û£¬Á½¸öµ¥ÒýºÅÏ൱ÓÚÒ»¸öµ¥ÒýºÅ£¬»¹ÓÐÒ»¸öµ¥ÒýºÅÊÇÁ¬½Ó×Ö·û´®ÓõÄ