我想用一条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
相关文档:
问:怎样在一个UPDATE语句中使用表B的三个列更新表A中的三个列?
答:对这个问题,您可以使用强大的关系代数。本页中的代码说明了如何组合使用from子句和JOIN操作,以达到用其他表中数据更新指定列的目的。在设计关系表达式时,您需要决定是否需要单一行匹配多个行(一对多关系),或者需要多个行匹配被联接表中的单一 ......
今天终于知道SQL 2005 怎么用了,感觉以前太懒了,明明想知道的东西可是因为已经有sql2000就懒得查。知识这东西是日积月累的,真正到用的时候才去补就已经晚了。
以前安装VS2005的时候就看到安装完了以后会有一个SQL2005,可是自己不会用,那个时候只 ......
SELECT * into newtable
from OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\aaaa.xls";User ID=Admin;Password=;Extended properties=Excel 11.0')...[Sheet1$]
/******* 导出到excel
exec master..xp_cmdshell 'bcp settledb.dbo.shanghu out c:\temp1.xls -c -q -s"gnetdata/gnetdata" - ......