一列变成两列的问题

orcd 2010-03-08 10:00:23
主要是想显示 包内容物 想在一张A4的纸上显示
表 t1

f1(包编号) f2(物品编号) f3(物品名称)
xxx item1
xxx item2

...
xxx item100
如果直接显示的话可能会显示成一个长条 在一张纸上显示不开可能会显示成下面的样式

假设是以f2排序 序号这一列需要新创建 原来的表中没有

xxx的包内容物

序号 item1
1 ....
...
100 item100


怎样得到这样的记录



序号 物品 序号 物品
1 item1 51 item51
.. ... ...
50 item50 100 item100

...全文
190 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoliaoyun 2010-03-08
  • 打赏
  • 举报
回复
SQL2005,2008

declare @t table(f1 int identity,f2 varchar(100))
insert @t select 'Name1'
insert @t select 'Name3'
insert @t select 'Name2'
insert @t select 'Name6'
insert @t select 'Name4'
insert @t select 'Name5'
insert @t select 'Name10'
insert @t select 'Name11'
insert @t select 'Name8'
insert @t select 'Name7'
insert @t select 'Name9'


;WITH temp
AS
(
SELECT ROW_NUMBER() OVER (ORDER BY f2)AS [序号],f2 AS [物品] FROM @t
)
SELECT * FROM
(
SELECT * FROM temp WHERE [序号] <= ((SELECT MAX([序号]) FROM temp) + 1 )/2
)A
LEFT JOIN
(
SELECT * FROM temp WHERE [序号] > ((SELECT MAX([序号]) FROM temp) + 1 )/2
)B ON A.[序号] + ((SELECT MAX([序号]) FROM temp) + 1 )/2 = B.[序号]
GO
brio8425 2010-03-08
  • 打赏
  • 举报
回复
引用 6 楼 chuifengde 的回复:
SQL codeselect 物品,序号=identity(int,1,1)into #from[Table]orderby 物品select*from
(selecttop50*from #orderby 序号)aaleftjoin
(select*from #where 序号>50)bbon aa.序号=bb.序号-50


就是序号必须按顺序梯增。。。。。
orcd 2010-03-08
  • 打赏
  • 举报
回复
物品的总记录不会大于100,只有两列显示,我先测试一下各位的语句,谢谢了。
chuifengde 2010-03-08
  • 打赏
  • 举报
回复
select 物品,序号=identity(int,1,1) into # from [Table] order by 物品

select * from
(select top 50 * from # order by 序号)aa
left join
(select * from # where 序号>50)bb
on aa.序号=bb.序号-50
playwarcraft 2010-03-08
  • 打赏
  • 举报
回复
水族杰纶 2010-03-08
  • 打赏
  • 举报
回复
僅供參考
declare @t table(ID int identity,[Name] varchar(100))
insert @t select 'Name1'
insert @t select 'Name2'
insert @t select 'Name3'
insert @t select 'Name4'
insert @t select 'Name5'
insert @t select 'Name6'
insert @t select 'Name7'
insert @t select 'Name8'
insert @t select 'Name9'
insert @t select 'Name10'
insert @t select 'Name11'
select
max(case when ID%2=1 then [Name] else null end),
max(case when ID%2=0 then [Name] else null end)
from @t
group by (ID+1)/2
/*
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
Name1 Name2
Name3 Name4
Name5 Name6
Name7 Name8
Name9 Name10
Name11 NULL
警告: 彙總或其他 SET 作業已刪除 Null 值。

(6 個資料列受到影響)

*/
jc_liumangtu 2010-03-08
  • 打赏
  • 举报
回复
能写,但是麻烦,而且具体需求还有待明确。要是有101到150 是不是就有第五第六列了。
帮顶吧
orcd 2010-03-08
  • 打赏
  • 举报
回复
我是想在数据库中处理创建临时表 把数据放进去 然后外部程序直接提取就行了。
东那个升 2010-03-08
  • 打赏
  • 举报
回复
这个应该是外部程序设置的吧。

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧