ACCESS有点难度的行列转换解决思路
ACCESS有点难度的行列转换
表结构如下:
code month result1 result2 result3
---------------------------------------------
张三 month1 a1 a2 a3
张三 month2 b1 b2 b3
李四 month1 c1 c2 c3
李四 month2 d1 d2 d3
需转换成以下格式:
code result month1 month2
----------------------------------------------
张三 result1 a1 b1
张三 result2 a2 b2
张三 result3 a3 b3
李四 result1 c1 d1
李四 result2 c2 d2
李四 result3 c3 d3
应该如何做呢?比较困扰。希望各位达人帮忙看看,拜谢!
------解决方案--------------------
表结构如下:
code month result1 result2 result3
---------------------------------------------
张三 month1 a1 a2 a3
张三 month2 b1 b2 b3
李四 month1 c1 c2 c3
李四 month2 d1 d2 d3
需转换成以下格式:
code result month1 month2
----------------------------------------------
张三 result1 a1 b1
张三 result2 a2 b2
张三 result3 a3 b3
李四 result1 c1 d1
李四 result2 c2 d2
李四 result3 c3 d3
应该如何做呢?比较困扰。希望各位达人帮忙看看,拜谢!
------解决方案--------------------
- SQL code
select code,rc, sum(iif([month]='month1', result1,0) as month1, sum(iif([month]='month2', result1,0) as month2 from( select code,'result1' as rc,[month],result1 from 表 union all select code,'result2' as rc,[month],result2 from 表 union all select code,'result3' as rc,[month],result3 from 表 ) group by code,rc
------解决方案--------------------
transform max(result1)
select code,bz from
查询3 group by code,bz
pivot month
查询3
SELECT code,month,result1,'r1' as bz from tt5
union all
SELECT code,month,result2,'r2' from tt5
UNION ALL SELECT code,month,result3,'r3' from tt5;