求教SQL查询的问题

jasonwuhc 2005-12-25 11:33:00
表中的记录为:

ID GP1 P1 GP2 P2
1 01 a 03 k
2 01 h 04 j
3 01 p 06 d
4 02 j 03 w
5 02 u 04 c
6 03 g 04 q
7 06 n 07 h

如何得到下表中GP1,GP2组成的组合?规则是按ID顺序来取组合,组合中的GP1,GP2不能重复。不用游标能实现吗?

ID GP1 P1 GP2 P2
1 01 a 03 k
5 02 u 04 c
7 06 n 07 h
...全文
306 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
海阔天空1977 2005-12-26
  • 打赏
  • 举报
回复
学习一下子吧!
hpym365 2005-12-25
  • 打赏
  • 举报
回复
不对不对 我看错了不好意识
hpym365 2005-12-25
  • 打赏
  • 举报
回复
select id,
gp1,
p1 =(select top 1 p1 from 表 group by gp1),
gp2=(select top 1 gp2 from 表 group by gp1),
p1 =(select top 1 p2 from 表 group by gp1)
from 表
group by gp1,id
mislrb 2005-12-25
  • 打赏
  • 举报
回复
改完整点:
declare @t table(id int,GP1 varchar(2),P1 varchar(1),GP2 varchar(2),P2 varchar(1))
declare @maxid int,@i int,@rc int
select @i=1,@maxid=(select max(id) from #t),@rc=(select count(id) from #t)
insert @t select * from #t where id=1
while @i<=@rc
begin
set @i=@i+1
if (@i>@maxid) break
insert @t
select * from #t
where id=@i
and gp1 not in (select gp1 from @t union select gp1=gp2 from @t)
and gp2 not in (select gp2 from @t union select gp2=gp1 from @t)
end
select * from @t

当然如果表没有ID,也可用游标实现
xkm611217 2005-12-25
  • 打赏
  • 举报
回复
改了改
declare @t table(id int,GP1 varchar(2),P1 varchar(1),GP2 varchar(2),P2 varchar(1))
declare @maxid int,@i int
select @i=1,@maxid=(select count(*) from #t)
insert @t select top 1 * from #t
while @i<=@maxid
begin
insert @t
select top 1 * from #t
where id not in(select id from @t)
and gp1 not in (select gp1 from @t)
and gp2 not in (select gp2 from @t)
set @i=@i+1
end
select * from @t
xkm611217 2005-12-25
  • 打赏
  • 举报
回复
mislrb(aben) 作的好呀 不过,@maxid=(select max(id) from #t),while @i<=@maxid
的条件值得考虑 要是不连续的ID很多 间距很大就进行了过多的循环 我觉得该用总行数作为循环条件 where里面加上ID不重复的条件 每次用TOP提取一行 这样效率高些
jasonwuhc 2005-12-25
  • 打赏
  • 举报
回复
多谢 mislrb(aben) ,不过要修改一下:

declare @t table(id int,GP1 varchar(2),P1 varchar(1),GP2 varchar(2),P2 varchar(1))
declare @maxid int,@i int
select @i=1,@maxid=(select max(id) from #t)
insert @t select * from #t where id=1
while @i<=@maxid
begin
set @i=@i+1
insert @t
select * from #t
where id=@i
and gp1 not in (select gp1 from @t union all select gp2 from @t)
and gp2 not in (select gp2 from @t union all select gp1 from @t)
end
select * from @t

不知道还有没有其它方法?
hpym365 2005-12-25
  • 打赏
  • 举报
回复
哇 学习学习
mislrb 2005-12-25
  • 打赏
  • 举报
回复
create table #t(id int identity(1,1),GP1 varchar(2),P1 varchar(1),GP2 varchar(2),P2 varchar(1))
insert #t
select '01', 'a', '03', 'k' union all
select '01', 'h', '04', 'j' union all
select '01', 'p', '06', 'd' union all
select '02', 'j', '03', 'w' union all
select '02', 'u', '04', 'c' union all
select '03', 'g', '04', 'q' union all
select '06', 'n', '07', 'h'

declare @t table(id int,GP1 varchar(2),P1 varchar(1),GP2 varchar(2),P2 varchar(1))
declare @maxid int,@i int
select @i=1,@maxid=(select max(id) from #t)
insert @t select * from #t where id=1
while @i<=@maxid
begin
set @i=@i+1
insert @t
select * from #t
where id=@i
and gp1 not in (select gp1 from @t)
and gp2 not in (select gp2 from @t)
end
select * from @t

drop table #t

/*
id GP1 P1 GP2 P2
----------- ---- ---- ---- ----
1 01 a 03 k
5 02 u 04 c
7 06 n 07 h
*/
jasonwuhc 2005-12-25
  • 打赏
  • 举报
回复
不是随机抽!是按ID顺序取数,例如:取得第一条记录后,得到GP1='01',GP2='03'.那么再取下一条记录时GP1,GP2中都不能取包含有'01','03'的记录,如此类推!
lxzm1001 2005-12-25
  • 打赏
  • 举报
回复
不会是随机抽三条记录吧
zjcxc 2005-12-25
  • 打赏
  • 举报
回复
没看出楼主的组合规律

22,298

社区成员

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

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