sqlÐÐÁл¥×ª(תÌû)
--ÐÐÁл¥×ª
/******************************************************************************************************************************************************
ÒÔѧÉú³É¼¨ÎªÀý×Ó£¬±È½ÏÐÎÏóÒ×¶®
ÕûÀíÈË£ºÖйú·ç(Roy)
ÈÕÆÚ:2008.06.06
******************************************************************************************************************************************************/
--1¡¢Ðл¥ÁÐ
--> --> (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
--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 when [Course]='ÓïÎÄ' then [Score] else 0 end)
from
Class
group by [Student]
GO
--2005·½·¨:
¶¯Ì¬:
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
Ïà¹ØÎĵµ£º
ORMϵͳ±ØÐë°ÑÊý¾Ý¿âÖеÄÊý¾Ýת»»ÎªÒµÎñÊý¾Ý£¬×ª»»µÄ·½·¨´óÖÂÓÐ3ÖÖ£¬±¾ÎľÍÊÔͼ¶ÔËüÃÇ×öһЩ¼òµ¥·ÖÎö¡£
1¡¢ÊôÐÔ·´Éä¡£¾ÍÊÇͨ¹ý·´É䣬»ñȡҵÎñʵÌåÀàµÄ¸÷¸öÊôÐÔ£¬È»ºóÔÙÉèÖÃÕâЩÊôÐÔµÄÖµ¡£Õâ¸ö·½·¨×î¼òµ¥¡¢×îÎȶ¨¡¢Í¨ÓÃÐÔ×îÇ¿¡¢¿Éά»¤ÐÔ×îÇ¿¡¢ÐÔÄÜ×î²î¡£ÀýÈçNHibernate¾ÍÊÇÓÃÕâÖÖ·½·¨ÊµÏֵģ¬Ëüͨ¹ýIGetterºÍISetter½Ó¿ÚÊµÏ ......
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Collections;
using System.Data.SqlClient;
/// <summary>
/// Êý¾Ý¿âµÄͨÓ÷ÃÎÊ´úÂë
/// ´ËÀàΪ³éÏóÀ࣬²»ÔÊÐíʵÀý»¯£¬ÔÚÓ¦ÓÃʱֱ½Óµ÷Óü´¿É
/// </summary>
public ab ......
Àí½âSQL SERVERÖÐËùÓÐÕߺͼܹ¹µÄÇø±ð
SQL SERVER2005½éÉÜÁ˼ܹ¹£¬¼Ü¹¹Ïà¶ÔÓÚÒÔǰ°æ±¾ÖеĶÔÏóËùÓÐÕß¡£±¾ÎĽ«½âÊÍÕâÁ½ÕßµÄÇø±ð£¬²¢Ï£ÍûÄܽ⿪ÄãÖÁ½ñÈԶԼܹ¹Ò»µãÀ§»ó¡£
¶ÔÏóËùÓÐÕß
ÒªÀí½âËùÓÐÕߺͼܹ¹Ö®¼äµÄÇø±ð£¬ÈÃÎÒÃÇÏÈ»¨µãʱ¼äÀ´¸´Ï°Ò»Ï¶ÔÏóµÄËùÓÐȨ¡£ÔÚSQL SERVER2000»òÒÔǰ°æ±¾Öд´½¨Ò»¸ö¶ÔÏ󣬶ÔÏó±ØÐëÒªÓÐÒ»¸öËù ......
SQL·ÖÒ³
ÍòÄÜ·ÖÒ³
.net´úÂë
select top ÿҳÏÔʾµÄ¼Ç¼Êý * from topic where id not in
(select top £¨µ±Ç°µÄÒ³Êý-1£©×ÿҳÏÔʾµÄ¼Ç¼Êý id from topic order by id desc)  ......
ÔÎijö´¦£ºhttp://www.dingos.cn/index.php?topic=1874.0
¶¨ÒåºÍÓ÷¨
CONVERT() º¯ÊýÊǰÑÈÕÆÚת»»ÎªÐÂÊý¾ÝÀàÐ͵ÄͨÓú¯Êý¡£
CONVERT() º¯Êý¿ÉÒÔÓò»Í¬µÄ¸ñʽÏÔʾÈÕÆÚ/ʱ¼äÊý¾Ý
Óï·¨
CONVERT(data_type(length),data_to_be_converted,style)
data_type(length) ¹æ¶¨Ä¿±êÊý¾ÝÀàÐÍ£¨´øÓпÉÑ¡µÄ³¤¶È£©¡£data_to_be_conv ......