有表:tb
列1 列2 列3 列4
1 2 f 4
1 d 4 4
2 3 4 5
f g e h
f z l h
相同的列1和相同的列4只显示一条
查询结果为:
1 2/d f/4 4
2 3 4 5
f g/z e/l h
12 个解决方案
#1
转到sql版块去问问!速度会快点!
#2
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([列1] varchar(1),[列2] varchar(1),[列3] varchar(1),[列4] varchar(1))
insert [TB]
select '1','2','f','4' union all
select '1','d','4','4' union all
select '2','3','4','5' union all
select 'f','g','e','h' union all
select 'f','z','l','h'
select * from [TB]
create function dbo.f_列1(@id varchar(10)) returns varchar(100)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + '/','') + 列2 from tb where 列1 = @id
return @str
end
go
create function dbo.f_列2(@id varchar(10)) returns varchar(100)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + '/','') + 列3 from tb where 列1 = @id
return @str
end
go
--调用函数
select 列1,dbo.f_列1(列1),dbo.f_列2(列1) from tb
group by 列1
/*
列1
---- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 2/d f/4
2 3 4
f g/z e/l
(所影响的行数为 3 行)
*/
drop function dbo.f_列1,dbo.f_列2
drop table tb
#3
果然,我SQL学的不好
#4
连问题都没有看懂..
#5
不错 好帖子友情帮顶
#6
if object_id('tb') is not null
drop table tb
create table tb(column1 varchar(1),column2 varchar(1),column3 varchar(1),column4 varchar(1))
insert tb
select '1','2','f','4' union all
select '1','d','4','4' union all
select '2','3','4','5' union all
select 'f','g','e','h' union all
select 'f','z','l','h'
select * from tb
create function dbo.f_strUnite(@column1 varchar(1),@column4 varchar(1)) --创建函数
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+'/'+column2 from --合并column2
(select * from tb where column1=@column1 and column4=@column4) a --满足条件column1与column4相同
set @str=stuff(@str,1,1,'')
return @str
end
create function dbo.f_strUnite1(@column1 varchar(1),@column4 varchar(1)) --创建函数
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+'/'+column3 from --合并column3
(select * from tb where column1=@column1 and column4=@column4) a --满足条件column1与column4相同
set @str=stuff(@str,1,1,'')
return @str
end
select distinct column1,column2=dbo.f_strUnite(column1,column4),column3=dbo.f_strUnite1(column1,column4),column4 from tb
#7
d
#9
这需求还真是..
#10
又来这儿问了
#11
头像好漂亮
#12
先顶下,晚上在来
#1
转到sql版块去问问!速度会快点!
#2
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([列1] varchar(1),[列2] varchar(1),[列3] varchar(1),[列4] varchar(1))
insert [TB]
select '1','2','f','4' union all
select '1','d','4','4' union all
select '2','3','4','5' union all
select 'f','g','e','h' union all
select 'f','z','l','h'
select * from [TB]
create function dbo.f_列1(@id varchar(10)) returns varchar(100)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + '/','') + 列2 from tb where 列1 = @id
return @str
end
go
create function dbo.f_列2(@id varchar(10)) returns varchar(100)
as
begin
declare @str varchar(1000)
select @str = isnull(@str + '/','') + 列3 from tb where 列1 = @id
return @str
end
go
--调用函数
select 列1,dbo.f_列1(列1),dbo.f_列2(列1) from tb
group by 列1
/*
列1
---- ---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 2/d f/4
2 3 4
f g/z e/l
(所影响的行数为 3 行)
*/
drop function dbo.f_列1,dbo.f_列2
drop table tb
#3
果然,我SQL学的不好
#4
连问题都没有看懂..
#5
不错 好帖子友情帮顶
#6
if object_id('tb') is not null
drop table tb
create table tb(column1 varchar(1),column2 varchar(1),column3 varchar(1),column4 varchar(1))
insert tb
select '1','2','f','4' union all
select '1','d','4','4' union all
select '2','3','4','5' union all
select 'f','g','e','h' union all
select 'f','z','l','h'
select * from tb
create function dbo.f_strUnite(@column1 varchar(1),@column4 varchar(1)) --创建函数
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+'/'+column2 from --合并column2
(select * from tb where column1=@column1 and column4=@column4) a --满足条件column1与column4相同
set @str=stuff(@str,1,1,'')
return @str
end
create function dbo.f_strUnite1(@column1 varchar(1),@column4 varchar(1)) --创建函数
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+'/'+column3 from --合并column3
(select * from tb where column1=@column1 and column4=@column4) a --满足条件column1与column4相同
set @str=stuff(@str,1,1,'')
return @str
end
select distinct column1,column2=dbo.f_strUnite(column1,column4),column3=dbo.f_strUnite1(column1,column4),column4 from tb
#7
d
#8
#9
这需求还真是..
#10
又来这儿问了
#11
头像好漂亮
#12
先顶下,晚上在来