交叉表转换思路(表1,和表2,为了直观简了一些字段)
表1:
|
用户类型 |
本月签约户数 | 累计到上月签约户数 |
| 工业用户 | 117 | 321 |
| 公福用户 | 119 | 327 |
| 老居民用户 | 116 | 318 |
| 商业用户 | 118 | 324 |
| 新建居民用户 | 115 | 315 |
表2:
| 数据类型 | 工业用户 | 公福用户 | 老居民用户 | 商业用户 | 新建居民用户 |
| 本月签约户数 | 117 | 119 | 116 | 118 | 115 |
| 累计到上月签约户数 | 321 | 327 | 318 | 324 | 315 |
要把表1转成表2的格式
第一,要变成这表数据表
| 用户类型 | 数据类型 | 数量 | 序 |
| 工业用户 | 本月签约户数 | 122 | 1 |
| 公福用户 | 本月签约户数 | 124 | 1 |
| 老居民用户 | 本月签约户数 | 121 | 1 |
| 商业用户 | 本月签约户数 | 123 | 1 |
| 新建居民用户 | 本月签约户数 | 120 | 1 |
| 工业用户 | 累计到上月签约户数 | 438 | 2 |
| 公福用户 | 累计到上月签约户数 | 446 | 2 |
| 老居民用户 | 累计到上月签约户数 | 434 | 2 |
| 商业用户 | 累计到上月签约户数 | 442 | 2 |
| 新建居民用户 | 累计到上月签约户数 | 430 | 2 |
declare @strYear varchar(4)
declare @strMonth varchar(2)
declare @strCompany varchar(50)
set @strYear='2009'
set @strMonth='5'
set @strCompany='南京中然'
select *
from
(
select 用户类型 ,数据类型='本月签约户数',数量=本月签约户数 ,1 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月签约户数',数量=累计到上月签约户数 ,2 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='本月安装户数',数量=本月安装户数 ,3 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月安装户数',数量=累计到上月安装户数 ,4 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月计划安装户数',数量=累计到上月计划安装户数 ,5 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='本月点火通气数',数量=本月点火通气数 ,6 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月点火通气数',数量=累计到上月点火通气数 ,7 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月计划点火通气数',数量=累计到上月计划点火通气数 ,8 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
) as vSplitMark
第二,进行透视转换
| 数据类型 | 工业用户 | 公福用户 | 老居民用户 | 商业用户 | 新建居民用户 |
| 本月签约户数 | 117 | 119 | 116 | 118 | 115 |
| 累计到上月签约户数 | 321 | 327 | 318 | 324 | 315 |
declare @strYear varchar(4)
declare @strMonth varchar(2)
declare @strCompany varchar(50)
set @strYear='2009'
set @strMonth='5'
set @strCompany='南京中然'
select
数据类型
, max(case 用户类型 when '工业用户' then 数量 else 0 end) as 工业用户
, max(case 用户类型 when '公福用户' then 数量 else 0 end) as 公福用户
, max(case 用户类型 when '老居民用户' then 数量 else 0 end) as 老居民用户
, max(case 用户类型 when '商业用户' then 数量 else 0 end) as 商业用户
, max(case 用户类型 when '新建居民用户' then 数量 else 0 end) as 新建居民用户
, max( 序 ) as 序
from
(
select 用户类型 ,数据类型='本月签约户数',数量=本月签约户数 ,1 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月签约户数',数量=累计到上月签约户数 ,2 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='本月安装户数',数量=本月安装户数 ,3 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月安装户数',数量=累计到上月安装户数 ,4 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月计划安装户数',数量=累计到上月计划安装户数 ,5 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='本月点火通气数',数量=本月点火通气数 ,6 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月点火通气数',数量=累计到上月点火通气数 ,7 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
union all
select 用户类型 ,数据类型='累计到上月计划点火通气数',数量=累计到上月计划点火通气数 ,8 as 序
from vMark where 年=@strYear and 月=@strMonth and 项目公司=@strCompany
) as vSplitMark
group by vSplitMark.数据类型
order by max( 序 )

浙公网安备 33010602011771号