一个比较复杂的SQL

viva369 2019-04-16 09:54:13
有2个表,脚本如下:

返回6个数字一组的字符串的所有数字组合,

要求单个表的数字不能重复,但2个表数字可以重复

例如返回正确如下
t1,7;t1,8;t1,9;t1,10;t1,11;t1,12
t1,7;t1,8;t1,9;t2,9;t2,10;t2,11

错误(t1出现2个7就错了):
t1,7;t1,7;t1,9;t1,10;t1,11;t1,12


create table #t1(price int)
create table #t2(price int)

insert #t1 select 6
insert #t1 select 7
insert #t1 select 8
insert #t1 select 9
insert #t1 select 10
insert #t1 select 11
insert #t1 select 12

insert #t2 select 6
insert #t2 select 7
insert #t2 select 8
insert #t2 select 9
insert #t2 select 10
insert #t2 select 11

select * from #t1
select * from #t2

实现方式存储过程都可以
...全文
74 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dear SQL(燊) 2019-04-17
  • 打赏
  • 举报
回复


create table #t1(price int)
create table #t2(price int)

insert #t1 select 6
insert #t1 select 7
insert #t1 select 8
insert #t1 select 9
insert #t1 select 10
insert #t1 select 11
insert #t1 select 12

insert #t2 select 6
insert #t2 select 7
insert #t2 select 8
insert #t2 select 9
insert #t2 select 10
insert #t2 select 11

if object_id('tempdb..#data') is not null drop table #data;
;
with list as(
	select tb='t1',* from #t1 
	UNION ALL
	select tb='t2',* from #t2
)
select *,rid=ROW_NUMBER()over(order by tb,price ) 
into #data
from list


select t1.tb+','+cast(t1.price as varchar(50))+';'+
	t2.tb+','+cast(t2.price as varchar(50))+';'+
	t3.tb+','+cast(t3.price as varchar(50))+';'+
	t4.tb+','+cast(t4.price as varchar(50))+';'+
	t5.tb+','+cast(t5.price as varchar(50))+';'+
	t6.tb+','+cast(t6.price as varchar(50))
from #data t1
inner join #data t2 on t1.rid<t2.rid
inner join #data t3 on t2.rid<t3.rid
inner join #data t4 on t3.rid<t4.rid
inner join #data t5 on t4.rid<t5.rid
inner join #data t6 on t5.rid<t6.rid

/*
结果只显示了部分

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
t1,6;t1,7;t1,8;t1,9;t1,10;t1,11
t1,6;t1,7;t1,8;t1,9;t1,10;t1,12
t1,6;t1,7;t1,8;t1,9;t1,10;t2,6
t1,6;t1,7;t1,8;t1,9;t1,10;t2,7
t1,6;t1,7;t1,8;t1,9;t1,10;t2,8

*/

二月十六 2019-04-17
  • 打赏
  • 举报
回复
返回6个数字一组的字符串的所有数字组合 组合的规则有吗? 还有结果中要带着t1 t2这样的字符串吗?
viva369 2019-04-17
  • 打赏
  • 举报
回复
引用 2 楼 Dear SQL 的回复:


create table #t1(price int)
create table #t2(price int)

insert #t1 select 6
insert #t1 select 7
insert #t1 select 8
insert #t1 select 9
insert #t1 select 10
insert #t1 select 11
insert #t1 select 12

insert #t2 select 6
insert #t2 select 7
insert #t2 select 8
insert #t2 select 9
insert #t2 select 10
insert #t2 select 11

if object_id('tempdb..#data') is not null drop table #data;
;
with list as(
	select tb='t1',* from #t1 
	UNION ALL
	select tb='t2',* from #t2
)
select *,rid=ROW_NUMBER()over(order by tb,price ) 
into #data
from list


select t1.tb+','+cast(t1.price as varchar(50))+';'+
	t2.tb+','+cast(t2.price as varchar(50))+';'+
	t3.tb+','+cast(t3.price as varchar(50))+';'+
	t4.tb+','+cast(t4.price as varchar(50))+';'+
	t5.tb+','+cast(t5.price as varchar(50))+';'+
	t6.tb+','+cast(t6.price as varchar(50))
from #data t1
inner join #data t2 on t1.rid<t2.rid
inner join #data t3 on t2.rid<t3.rid
inner join #data t4 on t3.rid<t4.rid
inner join #data t5 on t4.rid<t5.rid
inner join #data t6 on t5.rid<t6.rid

/*
结果只显示了部分

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
t1,6;t1,7;t1,8;t1,9;t1,10;t1,11
t1,6;t1,7;t1,8;t1,9;t1,10;t1,12
t1,6;t1,7;t1,8;t1,9;t1,10;t2,6
t1,6;t1,7;t1,8;t1,9;t1,10;t2,7
t1,6;t1,7;t1,8;t1,9;t1,10;t2,8

*/

谢谢,我先试一下
viva369 2019-04-17
  • 打赏
  • 举报
回复
引用 1 楼 二月十六 的回复:
返回6个数字一组的字符串的所有数字组合 组合的规则有吗? 还有结果中要带着t1 t2这样的字符串吗?
规则就是1个表的1个数字不能出现2次,其他随便组合

22,206

社区成员

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

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