create function getstr(@字段i char(10)) returns Nvarchar(1000) as begin declare @str Nvarchar(2000) set @str='' select @str=@str+rtrim(字段k) from table2 where 字段i=@字段i return @str endcreate view v_cceo as select 字段1,字段i,dbo.getstr(字段i) from table1 go --------------------------------------- select distinct a,b,c from table_name------------------------例--------------------------------------- create table table1 (a int,b varchar(50),c int) go
你没有项目字段吗????那你依据什么分组呢???编号 分类名称 分店A 分店B 分店C 项目 01 糖 30 40 50 1 02 菜 20 60 70 2 03 肉 10 20 30 1 04 鱼 10 20 30 3 05 包 10 20 35 2 06 果 20 25 30 3 07 豆 30 35 40 1create function getstr(@aaa int) returns varchar(2000) as begin declare @str varchar(2000) set @str='' select @str=@str+','+rtrim(编号) from 表 where 项目=@aaa set @str=right(@str,len(@str)-1) return @str end goselect sum(分店A) 分店A,sum(分店B) 分店B,sum(分店C) 分店C,dbo.getstr(编号) 承担项目 from 表 group by 项目
select sum(分店A) as 分店A,sum(分店B) as 分店B,sum(分店C) as 分店C from 表 where 承担项目 in ("01","02") union all select sum(分店A) as 分店A,sum(分店B) as 分店B,sum(分店C) as 分店C from 表 where 承担项目 in ("03","06","07") 这段代码只能用于你写的这些记录。这只能怪你没有设计分组依据。
returns Nvarchar(1000)
as
begin
declare @str Nvarchar(2000)
set @str=''
select @str=@str+rtrim(字段k) from table2
where 字段i=@字段i
return @str
endcreate view v_cceo
as
select 字段1,字段i,dbo.getstr(字段i) from table1
go
---------------------------------------
select distinct a,b,c from table_name------------------------例---------------------------------------
create table table1 (a int,b varchar(50),c int)
go
01 糖 30 40 50 1
02 菜 20 60 70 2
03 肉 10 20 30 1
04 鱼 10 20 30 3
05 包 10 20 35 2
06 果 20 25 30 3
07 豆 30 35 40 1create function getstr(@aaa int)
returns varchar(2000)
as
begin
declare @str varchar(2000)
set @str=''
select @str=@str+','+rtrim(编号) from 表
where 项目=@aaa
set @str=right(@str,len(@str)-1)
return @str
end
goselect sum(分店A) 分店A,sum(分店B) 分店B,sum(分店C) 分店C,dbo.getstr(编号) 承担项目 from 表 group by 项目
from 表 where 承担项目 in ("01","02")
union all
select sum(分店A) as 分店A,sum(分店B) as 分店B,sum(分店C) as 分店C
from 表 where 承担项目 in ("03","06","07")
这段代码只能用于你写的这些记录。这只能怪你没有设计分组依据。