我想用一条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操作,以达到用其他表中数据更新指定列的目的。在设计关系表达式时,您需要决定是否需要单一行匹配多个行(一对多关系),或者需要多个行匹配被联接表中的单一 ......
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" - ......
SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中 ......
Oracle SQL性能优化技巧大总结 收藏
(1)选择最有效率的表名顺序(只在基于规则的优化器中有效):
Oracle的解析器按照从右到左的顺序处理from子句中的表名,from子句中写在最后的表(基础表 driving table)将被最先处理,在from子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以 ......
行列转置的SQL语句
另一种行列转置 -- 动态 sql 交叉表
===========================================================
作者: wxy0327(http://wxy0327.itpub.net)
发表于: 2006.05.10 09:11
分类:
Oracle
出处: http://wxy0327.itpub.net/post/16888/88075
-------------------------- ......