CREATE TABLE T1(A NUMBER,
b NUMBER,
c NUMBER,
d NUMBER,
e NUMBER);
insert into t1 values(1,2,3,4,5);
select from t1;
select decode(A,1,'A',null) KEY ,decode(A,1,1,null) VALUE
from t1
union all
select decode(B,2,'B',null),decode(B,2,2,null)
from t1
union all
select decode(C,3,'C',null),decode(C,3,3,null)
from t1
union all
select decode(D,4,'D',null),decode(D,4,4,null)
from t1
union all
select decode(E,5,'E',null),decode(E,5,5,null)
from t1;
我仅提供思路给你,至于其它单位,可以单独摘录出来,union all
select 始发地 目的地 起步价,sum(case when 区间 > 0 and 区间 <= 20 then 单位价格 else '' end) '0-20(公斤)' ,sum(case when 区间 > 20 and 区间 <= 50 then 单位价格 else '' end) '20-50(公斤)' ,
sum(case when 区间 > 50 and 区间 <= 100 then 单位价格 else '' end) '50-100(公斤)' from (你的sql语句) t group by t始发地 ,t目的地 ,t起步价
第一:你的时间维度表基本没有意义,微软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
结果:
如果 salary 这一列没有确定的列名,又该怎么用? salary 是通过其他表统计出来的。
-----------------------------------
把其他表做一个内嵌或视图实现如
select
from
(select ID,sum(num) as num from test2 group by ID) as Test
SELECT
max(iif(OBJECT='基本工资',salary,0)) as 基本工资,
max(iif(OBJECT='加班工资',salary,0)) as 加班工资,
max(iif(OBJECT='伙食补贴',salary,0)) as 伙食补贴
FROM Test;
或
TRANSFORM MAX(salary)
SELECT 1 as ID FROM test group by 1
PIVOT object
SELECT 姓名,'语文' AS 学科,语文 FROM 表
UNION
SELECT 姓名,'数学',数学 FROM 表
UNION
SELECT 姓名,'物理',物理 FROM 表
UNION
SELECT 姓名,'化学',化学 FROM 表
以上就是关于sql 列转行全部的内容,包括:sql 列转行、MySql 列转行 行转列、SQLSERVER 列转行等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)