在Sql Server数据库中, 可否把表4读入数组,再把数组写入表5。如果能,是写在查询?还是写在哪里?

qq_33415720 2015-12-27 11:27:24
在Sql Server数据库中

表1
字段1
a1
a2
a3
a4
a5

表3
字段1 字段2 字段3
a1 a2 a3
a1 a3 a4
a1 a4 a5
a2 a3 a4
a2 a4 a5
a3 a4 a5
请教高手怎样把表1变成表3?可否通过表1变成数组,再把数组变成表3?


更重要的是

表4
字段1
A1
A2


An
其中n可能是5,也可能是50,100等,不固定。

表5
字段1 字段2 字段3
A1 A2 A3
A1 A3 A4
A1 A4 A5
… … …
A1 An-1 An
A2 A3 A4
A2 A4 A5
… … …
A2 An-1 An
… … …
… … …
An-2 An-1 An

请教高手怎样把表4变成表5,可否通过表4变成数组,再把数组变成表5?
...全文
232 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33415720 2015-12-30
  • 打赏
  • 举报
回复
请教各位高手,本问题可否在Access上解决?
zbdzjx 2015-12-29
  • 打赏
  • 举报
回复
噢,上面的,可以简化,不用生成ROW_NUMBER()。
with table1 as
(
select 'A1' 身高, 'A班' 班级 union all
select 'A2' 身高, 'A班' 班级 union all
select 'A3' 身高, 'A班' 班级 union all
select 'A4' 身高, 'A班' 班级 union all
select 'A5' 身高, 'A班' 班级 union all
select 'B1' 身高, 'B班' 班级 union all
select 'B2' 身高, 'B班' 班级 union all
select 'B3' 身高, 'B班' 班级 union all
select 'C1' 身高, 'C班' 班级 union all
select 'C2' 身高, 'C班' 班级 union all
select 'C3' 身高, 'C班' 班级 union all
select 'C4' 身高, 'C班' 班级 union all
select 'C5' 身高, 'C班' 班级 union all
select 'C6' 身高, 'C班' 班级
)
select a.身高 身高矮, b.身高 身高中, c.身高 身高高, a.班级 from table1 a 
inner join table1 b on a.身高<b.身高 and a.班级=b.班级
inner join table1 c on b.身高<c.身高 and b.班级=c.班级 
order by a.班级, a.身高, b.身高, c.身高
zbdzjx 2015-12-29
  • 打赏
  • 举报
回复
with table1 as
(
select 'A1' 身高, 'A班' 班级 union all
select 'A2' 身高, 'A班' 班级 union all
select 'A3' 身高, 'A班' 班级 union all
select 'A4' 身高, 'A班' 班级 union all
select 'A5' 身高, 'A班' 班级 union all
select 'B1' 身高, 'B班' 班级 union all
select 'B2' 身高, 'B班' 班级 union all
select 'B3' 身高, 'B班' 班级 union all
select 'C1' 身高, 'C班' 班级 union all
select 'C2' 身高, 'C班' 班级 union all
select 'C3' 身高, 'C班' 班级 union all
select 'C4' 身高, 'C班' 班级 union all
select 'C5' 身高, 'C班' 班级 union all
select 'C6' 身高, 'C班' 班级
)
, table2 as
(
select ROW_NUMBER() OVER(PARTITION BY 班级 ORDER BY 身高) rn, 身高, 班级 from table1
)
select a.身高 身高矮, b.身高 身高中, c.身高 身高高, a.班级 from table2 a 
inner join table2 b on a.rn<b.rn and a.班级=b.班级
inner join table2 c on b.rn<c.rn and b.班级=c.班级 
order by a.班级, a.身高, b.身高, c.身高
qq_33415720 2015-12-29
  • 打赏
  • 举报
回复
zbdzjx不但技术高超,还诲人不倦。好!
zbdzjx 2015-12-28
  • 打赏
  • 举报
回复
引用 3 楼 qq_33415720 的回复:
zbdzjx实在高明,谢谢! 请教,如何把查询结果 c1 c1 c1 A1 A2 A3 A1 A3 A4 A1 A4 A5 A2 A3 A4 A2 A4 A5 A3 A4 A5 写入表3?谢谢! 我一用insert into,就出错。
如果表1已经存在,就不用with那一段,table1改成表1。 如果表已经存在,就是insert into,如果表不存在,就是select ...into...
LongRui888 2015-12-28
  • 打赏
  • 举报
回复
你这个有什么具体的规律吗,我看着觉得有点乱。。。
qq_33415720 2015-12-28
  • 打赏
  • 举报
回复
zbdzjx实在高明,谢谢! 请教,如何把查询结果 c1 c1 c1 A1 A2 A3 A1 A3 A4 A1 A4 A5 A2 A3 A4 A2 A4 A5 A3 A4 A5 写入表3?谢谢! 我一用insert into,就出错。
qq_33415720 2015-12-28
  • 打赏
  • 举报
回复
zbdzjx确实高明 再请教,Ai是A班同学身高,Bi是B班同学身高,Ci是C班同学身高, 按照您#7楼的排法,怎样把表1变成表2 谢谢! 表1 身高 班级 A1 A班 A2 A班 A3 A班 A4 A班 A5 A班 B1 B班 B2 B班 B3 B班 C1 C班 C2 C班 C3 C班 C4 C班 C5 C班 C6 C班 表2 身高矮 身高中 身高高 班级 A1 A2 A3 A班 A1 A2 A4 A班 A1 A2 A5 A班 A1 A3 A4 A班 A1 A3 A5 A班 A1 A4 A5 A班 A2 A3 A4 A班 A2 A3 A5 A班 A2 A4 A5 A班 A3 A4 A5 A班 B1 B2 B3 B班 C1 C2 C3 C班 C1 C2 C4 C班 C1 C2 C5 C班 C1 C2 C6 C班 C1 C3 C4 C班 C1 C3 C5 C班 C1 C3 C6 C班 C1 C4 C5 C班 C1 C4 C6 C班 C1 C5 C6 C班 C2 C3 C4 C班 C2 C3 C5 C班 C2 C3 C6 C班 C2 C4 C5 C班 C2 C4 C6 C班 C2 C5 C6 C班 C3 C4 C5 C班 C3 C4 C6 C班 C3 C5 C6 C班 C4 C5 C6 C班
xiaoxiangqing 2015-12-28
  • 打赏
  • 举报
回复
用前台处理简单些吧
zbdzjx 2015-12-28
  • 打赏
  • 举报
回复
如下语句,其中,id必须要连续的,如果没有此列,或是不连续,用ROW_NUMBER()生成一个。
with table1 as
(
select 1 id, 'A1' c1 union all
select 2 id, 'A2' union all
select 3 id, 'A3' union all
select 4 id, 'A4' union all
select 5 id, 'A5'
)
select a.c1, b.c1, c.c1 from table1 a
left join table1 b on a.id<b.id
inner join table1 c on b.id=c.id-1
order by a.id, b.id
zbdzjx 2015-12-28
  • 打赏
  • 举报
回复
with table1 as
(
select 1 id, 'A1' c1 union all
select 2 id, 'A2' union all
select 3 id, 'A3' union all
select 4 id, 'A4' union all
select 5 id, 'A5'
)
select a.c1, b.c1, c.c1 from table1 a
inner join table1 b on a.id<b.id
inner join table1 c on b.id<c.id
order by a.id, b.id, c.id
qq_33415720 2015-12-28
  • 打赏
  • 举报
回复
引用 4 楼 yupeigu 的回复:
你这个有什么具体的规律吗,我看着觉得有点乱。。。
谢谢关注 我确实乱了,有错误。 应该是这样: 我把问题形象化,表1是一个班级同学的身高,从矮到高。想从中取3人按矮中高分别写入字段1、字段2、字段3的每行中变成表2(只要每行字段1中的身高矮、字段2中、字段3高即可,不与其他行对比),把所有的排列组合都弄出来。 表1 身高 A1 A2 ... An 如果班级有5人,则 表2 字段1 字段2 字段3 A1 A2 A3 A1 A2 A4 A1 A2 A5 A1 A3 A4 A1 A3 A5 A1 A4 A5 A2 A3 A4 A2 A3 A5 A3 A4 A5 如果班级有n人, 则字段1范围是Ai,i=1到n-2 则字段2范围是Aj,j=i+1到n-1 则字段3范围是Ak,k=j+1到n 我特别想知道,任意n(比如10,20,40),都能从表1写入表2 谢谢!

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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