小弟现在有一个表的记录为:
编号 类型 数量
001 电影碟 3
001 连续剧 1
......
002 电影碟 4
002 连续剧 1
.....
现在要得到以下结果
编号 类型描述
001 电影碟 3 连续剧 1.....
002 电影碟 4 连续剧 1.....
请问这样获得这样的结果???
编号 类型 数量
001 电影碟 3
001 连续剧 1
......
002 电影碟 4
002 连续剧 1
.....
现在要得到以下结果
编号 类型描述
001 电影碟 3 连续剧 1.....
002 电影碟 4 连续剧 1.....
请问这样获得这样的结果???
解决方案 »
- 检索最近记录
- Mssql2005 xml 查询 使用 exist
- 一个租赁合同表,一个铺位表,合同表的“铺位”字段里要包含多个铺位。
- 急急急,windows server 2003上安装sql2000问题?
- 删除重复纪录的问题
- 如何通过变量动态确定 select 的字段
- 求SQL语句一条
- select * from table where intId in(这里varchar型的'1,2,3,4')怎么才能执行?
- 在数据库里面的性别我是用bit的,我想在查询返回时是男或女,这样的存储过程怎么写?
- 如何将日文、韩文存进ORACLE数据库?
- 求佛系列:SQL 查询
- 这个触发器为什么会提示“列名ID不明角”
insert into tb select '001','电影碟',3
insert into tb select '001','连续剧',1
insert into tb select '002','电影碟',4
insert into tb select '002','连续剧',1create function F_Str(@编号 varchar(30))
returns nvarchar(4000)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+类型+''+ltrim(数量) from tb where 编号=@编号
return @S
endselect 编号,dbo.f_str(编号) as 类型描述 from tb
group by 编号
If object_id('ta') is not null
Drop table ta
Go
Create table ta(编号 varchar(4),类型 varchar(6),数量 int)
Go
Insert into ta
select '001','电影碟',3 union all
select '001','连续剧',1 union all
select '002','电影碟',4 union all
select '002','连续剧',1
Go
--Start
create function f_str(@id varchar(4))
returns varchar(100)
as
begin
declare @s varchar(100)
select @s = isnull(@s+ ' ','') + 类型+ ' '+ ltrim(数量)
from ta
where 编号 = @id
return @s
end
goselect 编号,dbo.f_str(编号) 类型描述
from ta
group by 编号drop function f_str
--Result:
/*
编号 类型描述
---- ----------------------------------------------------------------------------------------------------
001 电影碟 3 连续剧 1
002 电影碟 4 连续剧 1(所影响的行数为 2 行)*/
--End
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (编号 varchar(3),类型 varchar(6),数量 int)
insert into [tb]
select '001','电影碟',3 union all
select '001','连续剧',1 union all
select '002','电影碟',4 union all
select '002','连续剧',1select distinct 编号,dbo.fn_gettypenum(编号) from [tb]
/*
-------
001 电影碟 3 连续剧 1
002 电影碟 4 连续剧 1
*/
as--自定义函数
create function dbo.fn_gettypenum(@str varchar(200))
returns varchar(7000)
as
begin
declare @s varchar(7000) set @s=''
select @s =@s +' '+cast(类型 as varchar(200))+' '+cast(数量 as varchar(200))+' ' from [tb] where 编号=@str
if len(@s)>0 set @s=ltrim(left(@s,len(@s)))
return @s
end
--合并分拆表
/******************************************************************************************************************************************************
合并分拆表数据整理人:中国风(Roy)日期:2008.06.06
******************************************************************************************************************************************************/--> --> (Roy)生成測試數據
if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[Col2] nvarchar(1))
Insert Tab
select 1,N'a' union all
select 1,N'b' union all
select 1,N'c' union all
select 2,N'd' union all
select 2,N'e' union all
select 3,N'f'
Go合并表:SQL2000用函数:go
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@Col1 int)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
return @S
end
go
Select distinct Col1,Col2=dbo.F_Str(Col1) from TabgoSQL2005用XML:方法1:select
a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'')
from
(select distinct COl1 from Tab) a
Cross apply
(select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b方法2:select
a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44))
from
(select distinct COl1 from Tab) a
cross apply
(select Col2=(select COl2 from Tab where COl1=a.COl1 FOR XML AUTO, TYPE)
.query('<Tab>
{for $i in /Tab[position()<last()]/@COl2 return concat(string($i),",")}
{concat("",string(/Tab[last()]/@COl2))}
</Tab>')
)b
--sql2005
if object_id('tb') is not null
drop table tb
go
create table tb(编号 varchar(30),类型 varchar(30),数量 int)
insert into tb select '001','电影碟',3
insert into tb select '001','连续剧',1
insert into tb select '002','电影碟',4
insert into tb select '002','连续剧',1select
a.编号,类型描述=stuff(b.类型.value('/R[1]','nvarchar(max)'),1,1,'')+' '
from
(select distinct 编号 from tb) a
Cross apply
(select 类型=(select N','+类型+' '+ltrim(数量) from tb where 编号=a.编号 For XML PATH(''), ROOT('R'), TYPE))b编号 类型描述
001 电影碟 3,连续剧 1
002 电影碟 4,连续剧 1
不过得建立一个过程。
--建表
if object_id('test') is not null drop table test
go
create table test ([编号] varchar(3) ,[类型] varchar(10),[数量] int)
insert into test values ('001','电影碟', 3 )
insert into test values ('001','连续剧' , 1 )
insert into test values ('002' , '电影碟' , 4)
insert into test values ('002' , '连续剧' , 1 )
go--建立函数
create function join_str(@content varchar(100))
returns varchar(2000)
as
begin
declare @str varchar(2000)
set @str=''
select @str=@str+space(5)+rtrim([类型])+space(5)+convert(char(3),[数量]) from test where [编号]=@content
select @str=right(@str,len(@str)-1)
return @str
end
go--调用:
select [编号],dbo.join_str([编号])as [描述] from test group by [编号]结果:
编号 描述
---- ------------------------------------------------------------------------------------------001 电影碟 3 连续剧 1
002 电影碟 4 连续剧 1 (2 row(s) affected)