我想用一条SQL显示这样的结果
不知道这样的要求能不能实现?
比如我有一张表T1,里面只有一个字段1
里面有100条记录,如下所示:
字段1
A1
A2
A3
A4
...
A100
我想用一条SQL显示这样的结果
第一列 第二列 ... 第十列
A1 A11 A91
A2 A12 A92
A3 A13 A93
... ... ...
A10 A20 A100
不知可否实现?当然,也不一定分成十列,可能是五列等等,请高手赐教。谢谢
--假设字段不存在相同值.
select
max(case (px - 1) % 10 when 0 then 字段1 else '' end) [c1],
max(case (px - 1) % 10 when 1 then 字段1 else '' end) [c2],
max(case (px - 1) % 10 when 2 then 字段1 else '' end) [c3],
max(case (px - 1) % 10 when 3 then 字段1 else '' end) [c4],
max(case (px - 1) % 10 when 4 then 字段1 else '' end) [c5],
max(case (px - 1) % 10 when 5 then 字段1 else '' end) [c6],
max(case (px - 1) % 10 when 6 then 字段1 else '' end) [c7],
max(case (px - 1) % 10 when 7 then 字段1 else '' end) [c8],
max(case (px - 1) % 10 when 8 then 字段1 else '' end) [c9],
max(case (px - 1) % 10 when 9 then 字段1 else '' end) [c10]
from
(
select * , px = (select count(1) from t1 where 字段1 < t.字段1) + 1 from t1 t
) m
group by (px - 1) / 10
--如果字段存在相同值.
--sql 2000需要使用临时表
select * , px = identity(int,1,1) into tmp from t1
select
max(case (px - 1) % 10 when 0 then 字段1 else '' end) [c1],
max(case (px - 1) % 10 when 1 then 字段1 else '' end) [c2],
max(case (px - 1) % 10 when 2 then 字段1 else '' end) [c3],
m
相关文档:
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
SQL SERVER DBCC命令解释
八点钟起床一直搞到现在,好多还不太记得,先放上来以后慢慢修改
2007-05-01 12:56:28
---------------------
--1 dbcc trraceon DBCC TRACEOFF
--对于数据库死锁,通常可以通过TRACE FLAG 1204、1205、1206,检查ERRORLOG里面的输出,和分析SQLTRACE的执行上下文判断死锁问题的来由。
--T ......
今天终于知道SQL 2005 怎么用了,感觉以前太懒了,明明想知道的东西可是因为已经有sql2000就懒得查。知识这东西是日积月累的,真正到用的时候才去补就已经晚了。
以前安装VS2005的时候就看到安装完了以后会有一个SQL2005,可是自己不会用,那个时候只 ......
清空日志:
dump transaction 库名 with no_log
截断日志:
backup log 库名 with no_log
压缩数据库:
dbcc shrinkdatabase (库名, 目标比率)
压缩数据库文件:
dbcc shrinkfile (文件名或ID, 目标大小)
文件名或ID可以通过系统表sysfiles查找,如果不指定目标大小SQL Server将最大限度的压缩数据库文件。
查看压 ......
如果原来的数据库可用,分离数据库后,只附加数据文件,不附加日志
如果不可用,只能慢慢等还原操作完成了
日志文件太大了,应该定期整理日志
--压缩日志及数据库文件大小
&n ......