select ID,group_concat(NAME) as NAME from table group by ID;
select ID,group_concat(NAME SEPARATOR ';') as NAME from a group by ID;
第一:你的时间维度表基本没有意义,微软SSIS中心认为时间维度至少由日期构成主键。可以认为是最小基本业务颗粒。
来个Sample,更复杂的在我空间里。但是道理是一样的。都是借助动态SQL和一些函数。
----------------------------------------------------------------/
作者:Edwin
数据库:SQL SERVER 2005+
作用:指定时间区间的自然周有几天,如果垮年度,由外围验证
Version 10
Copyright (c) 2015, SQL SERVER 2008
/
----------------------------------------------------------------
/参数设定区域,参数为开始时间和结束时间/
----------------------------------------------------------------
declare @FDate DateTime set @FDate = '2014-09-01 00:00:00000'
declare @Edate DateTime set @Edate = '2015-01-01 00:00:00000'
----------------------------------------------------------------
/SQL主体/
----------------------------------------------------------------
declare @WeekHeader nvarchar(max)
select @WeekHeader = coalesce(@WeekHeader+',['+cast(WeekOfYear as varchar)+']','['+cast(WeekOfYear as varchar)+ ']')
from
(
select WeekOfYear from ComnCalendar where DatePerDay>=@FDate and DatePerDay<@Edate group by WeekOfYear
) M
declare @PivotSQL nvarchar(max) set @PivotSQL=N'
select
Year as 年份,'+@WeekHeader+'
from
(
select [Year],WeekOfYear,DatePerDay from [DT_WareHouse][Comn][Calendar] where DatePerDay>=@FDate and DatePerDay<@Edate
) M
pivot
(
count(DatePerDay) for [WeekOfYear] in('+@WeekHeader+')
) PVT'
exec sp_executesql @PivotSQL,N'@FDate datetime,@Edate datetime',@FDate,@Edate
执行结果:
在看一下时间维度表:
至于PVT标题别名问题,这个可以在时间维度表中创建字符串类型的第几周等样式的列来完成。
这种方式比较简单。
或是
----------------------------------------------------------------/
作者:Edwin
数据库:SQL SERVER 2005+
作用:指定时间区间的自然周销售,统计周期为某一年,如果垮年度,由外围验证
Version 10
Copyright (c) 2015, SQL SERVER 2008
/
----------------------------------------------------------------
/参数设定区域,参数为开始时间和结束时间/
----------------------------------------------------------------
declare @FDate DateTime set @FDate = '2014-09-01 00:00:00000'
declare @Edate DateTime set @Edate = '2015-01-01 00:00:00000'
----------------------------------------------------------------
/SQL主体/
----------------------------------------------------------------
declare @WeekHeader nvarchar(max)
select @WeekHeader = coalesce(@WeekHeader+',['+cast(WeekOfYear as varchar)+']','['+cast(WeekOfYear as varchar)+ ']')
from
(
select ('第'+cast(WeekOfYear as varchar)+'周') as WeekOfYear from ComnCalendar where DatePerDay>=@FDate and DatePerDay<@Edate group by WeekOfYear
) M
declare @PivotSQL nvarchar(max) set @PivotSQL=N'
select
Year as 年份,'+@WeekHeader+'
from
(
select [Year],(''第''+cast(WeekOfYear as varchar)+''周'') as WeekOfYear,DatePerDay from [DT_WareHouse][Comn][Calendar] where DatePerDay>=@FDate and DatePerDay<@Edate
) M
pivot
(
count(DatePerDay) for [WeekOfYear] in('+@WeekHeader+')
) PVT'
exec sp_executesql @PivotSQL,N'@FDate datetime,@Edate datetime',@FDate,@Edate
结果:
DROP TABLE A
CREATE table A(
id varchar(10)
)
insert into A SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
第一种:
DECLARE @STR VARCHAR(8000)
SELECT @STR=ISNULL(@STR+',','')+id FROM (SELECT DISTINCT id FROM A)AS T
SELECT @STR
第二种:
declare @STR varchar(8000)
select @STR = coalesce(@STR+ ',' , '') + id from A
select @STR
第三种 :
FOR XML PATH
declare @table table
(
name varchar(10)
)
insert into @table values
('A'),
('B'),
('C')
select STUFF((
SELECT ','+name
FROM @table
ORDER BY name
FOR XML PATH('')) ,1,1,space(0)) as str
以上就是关于mysql中列转行,并多个字段转到一行中的一个字段上,中间用逗号分开全部的内容,包括:mysql中列转行,并多个字段转到一行中的一个字段上,中间用逗号分开、SQLSERVER 列转行、sql2008列转行语句等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)