求sql 语句!

lslyl 2004-04-16 01:22:22
我有一个表如下。

id name type
1 abc x
2 bcd x
3 aa y

我想得到下面的查询结果sql 语句怎么写。

name type

abc+bcd x
aa y

谢了。请高手告之
...全文
25 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
youngby 2004-04-17
  • 打赏
  • 举报
回复
说个没有效率的办法,先如下:

SELECT NAME INTO X FROM TABLE WHERE TYPE='X'

SELECT NAME INTO Y FROM TABLE WHERE TYPE='Y'

在用游标加
~_~

CCEO 2004-04-17
  • 打赏
  • 举报
回复
可以建视图

create view v_Names
as

select name=dbo.f_str(type),type from 表 group by type

go

select * from v_Names

jiangchuandong 2004-04-17
  • 打赏
  • 举报
回复
学习
lslyl 2004-04-17
  • 打赏
  • 举报
回复
can it buliding view ! thanks!
lslyl 2004-04-17
  • 打赏
  • 举报
回复
thanks
internetcsdn 2004-04-16
  • 打赏
  • 举报
回复
学习
zjcxc 元老 2004-04-16
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(id int,name varchar(10),type char(1))
insert 表 select 1,'abc','x'
union all select 2,'bcd','x'
union all select 3,'aa' ,'y'
go

--创建处理函数
create function f_str(@type char(1))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+'+'+name from 表 where type=@type
return(substring(@r,2,8000))
end
go

--调用实现查询
select name=dbo.f_str(type),type from 表 group by type
go

--删除测试
drop table 表
drop function f_str

/*--测试结果

name type
-------------------- ----
abc+bcd x
aa y

(所影响的行数为 2 行)

--*/
zjcxc 元老 2004-04-16
  • 打赏
  • 举报
回复
--还少写了一个条件

--创建处理函数
create function f_str(@type char(1))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+'+'+name from 表 where type=@type
return(substring(@r,2,8000))
end
go

--调用实现查询
select name=dbo.f_str(type),type from 表 group by type
zjcxc 元老 2004-04-16
  • 打赏
  • 举报
回复
--上面错了一点:

--创建处理函数
create function f_str(@type char(1))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+'+'+name from 表
return(substring(@r,2,8000))
end
go

--调用实现查询
select name=dbo.f_str(type),type from 表 group by type
zjcxc 元老 2004-04-16
  • 打赏
  • 举报
回复
--创建处理函数
create function f_str(@type char(1))
returns varchar(8000)
as
begin
declare @r varcahr(8000)
set @r=''
select @r=@r+'+'+name from 表
return(substring(@r,2,8000))
end
go

--调用实现查询
select name=dbo.f_str(type),type from 表 group by type

34,590

社区成员

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

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