我想用一条SQL显示这样的结果

本文介绍如何通过SQL语句将一张包含单一字段的长表格转换为宽表格形式,即从垂直排列的数据转换为水平排列,例如将100条记录重新组织成10列的形式。文中提供了适用于不同情况下的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],
 
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 tmp where 字段1 < t.字段1) + 1 from tmp t
) m
group by (px - 1) / 10

--不管字段是否存在相同值,sql 2005都可用row_number()解决
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 = row_number() over(order by 字段1) from t1
) m
group by (px - 1) / 10

 

 

 

http://topic.youkuaiyun.com/u/20091216/09/190c8c8b-de5d-4ea9-9866-3956cb0d1f8a.html?48518

您好,我是ChitGPT,根据您的问题,我理解您想往数据库中添加8条记录,并且想知道如何在Java中执行这个操作并显示添加的结果。如果我的理解有误,请告诉我。 在Java中执行添加操作,通常需要使用JDBC来连接数据库并执行SQL语句。具体来说,您需要编如下代码: ``` import java.sql.*; public class Example { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // 连接数据库 Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); // 执行添加操作 stmt = conn.createStatement(); int count = 0; for (int i = 0; i < 8; i++) { String sql = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')"; count += stmt.executeUpdate(sql); } // 显示添加结果 System.out.println("添加了" + count + "条记录"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭连接 try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 上述代码中,我们首先通过JDBC连接到MySQL数据库,然后通过循环执行8次添加操作,并统计添加的记录数。最后输出添加的结果。请注意,上述代码中的table_name、column1、column2、value1、value2需要替换您实际的表名和字段名和值。 希望能够帮助您解决问题。如果还有其他问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值