表A
2014,1,A,5
2014,1,A,9
2014,2,A,6
2014,3,B,8
得出表B
2014,1,5,9
2014,2,6,0
2014,3,0,8
即 2014,1,A均相同则将最后的数字放在两列
如果只有A或者B,则没有的数字列填0
一列数字要变成2列。
------解决方案--------------------
2014,2,6,0
2014,3,0,8
这个为什么一个是0在后面,而一个是0在前面嗯
------解决方案--------------------
Select
A1.c1 , A1.c2 , A1.c3, A2.c3
Form
( Select * from A where c3 ="BJ" ) as A1 ,
( Select * from A where c3 ="sh" ) as A2
where
A1.c1 = A2.c1 and A1.c2 = A2.c2 and A1.c3 = A2.c3
------解决方案--------------------
你看看是这个吗:
--drop table t
create table t(
年份 int,月份 int,
地址 varchar(10),类型 varchar(20),数值 int)
insert into t
select 2014,1,'北京','电视机',20 union all
select 2014,1,'上海','电视机',44 union all
select 2014,1,'北京','洗衣机',40 union all
select 2014,1,'上海','手机',50 union all
select 2014,2,'北京','手机',20 union all
select 2014,2,'上海','手机',30 union all
select 2014,3,'北京','加热器',50 union all
select 2014,3,'上海','除湿机',60
go
select 年份,月份,类型,
max(case when 地址='北京' then 数值 else 0 end) '数值-北京',
max(case when 地址='上海' then 数值 else 0 end) '数值-上海'
from t
group by 年份,月份,类型
/*
年份 月份 类型 数值-北京 数值-上海
2014 1 电视机 20 44
2014 1 手机 0 50
2014 1 洗衣机 40 0
2014 2 手机 20 30
2014 3 除湿机 0 60
2014 3 加热器 50 0
*/