求助一个行列转化

yangshaohong890620 2012-10-09 11:27:30
数据如下:
Organization Province P93 P97
社会单位 101 7.03 7.66
中石化 101 7.08 7.67
中石油 101 7.11 7.67
社会单位 102 7.03 7.66
中石化 102 7.08 7.67
中石油 102 7.11 7.67

经行转列,数据变化成:
Province 社会单位P93 中石化P93 中石油P93 社会单位P97 中石化P97 中石油P97
101 7.03 7.08 7.11 7.66 7.67 7.67
102 7.03 7.08 7.11 7.66 7.67 7.67
...全文
74 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
-Tracy-McGrady- 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 4 楼 的回复:

上面的是我在帮忙回答问题的时候想到的两个办法,我想用pivot,但是不知道要怎么用,请问有谁会吗?或者是还有其他的一些方法。

pivot 要p两次
[/Quote]
怎么P ? 我表示不会啊
汤姆克鲁斯 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

上面的是我在帮忙回答问题的时候想到的两个办法,我想用pivot,但是不知道要怎么用,请问有谁会吗?或者是还有其他的一些方法。
[/Quote]
pivot 要p两次
-Tracy-McGrady- 2012-10-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

还有一个最简单的办法,将数据导出到Excel,复制>>选择粘贴性>>倒置
[/Quote]
我说的是SQL语句
renolit 2012-10-09
  • 打赏
  • 举报
回复
还有一个最简单的办法,将数据导出到Excel,复制>>选择粘贴性>>倒置
-Tracy-McGrady- 2012-10-09
  • 打赏
  • 举报
回复
上面的是我在帮忙回答问题的时候想到的两个办法,我想用pivot,但是不知道要怎么用,请问有谁会吗?或者是还有其他的一些方法。
汤姆克鲁斯 2012-10-09
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
CREATE TABLE [tb]([Organization] VARCHAR(8),[Province] INT,[P93] NUMERIC(3,2),[P97] NUMERIC(3,2))
INSERT [tb]
SELECT '社会单位',101,7.03,7.66 UNION ALL
SELECT '中石化',101,7.08,7.67 UNION ALL
SELECT '中石油',101,7.11,7.67 UNION ALL
SELECT '社会单位',102,7.03,7.66 UNION ALL
SELECT '中石化',102,7.08,7.67 UNION ALL
SELECT '中石油',102,7.11,7.67
--------------开始查询--------------------------
DECLARE @s VARCHAR(max)
SELECT @s=ISNULL(@s+',','')+QUOTENAME(Organization+'P93')+'=max(case when [Organization]='''+LTRIM([Organization])+''' then [P93] else 0 end)'
FROM [tb]
SELECT @s=ISNULL(@s+',','')+QUOTENAME(Organization+'P97')+'=max(case when [Organization]='''+LTRIM([Organization])+''' then [P97] else 0 end)'
FROM [tb]
GROUP BY [Organization]
SELECT @s='SELECT [Province],'+@s+'FROM [tb] GROUP BY Province'
--PRINT @s
EXEC(@s)


----------------结果----------------------------
/*
Province 社会单位P93 中石化P93 中石油P93 社会单位P93 中石化P93 中石油P93 社会单位P97 中石化P97 中石油P97
101 7.03 7.08 7.11 7.03 7.08 7.11 7.66 7.67 7.67
102 7.03 7.08 7.11 7.03 7.08 7.11 7.66 7.67 7.67
*/
-Tracy-McGrady- 2012-10-09
  • 打赏
  • 举报
回复

--测试数据
create table testTable(Organization nvarchar(10),Province varchar(5),P93 float,P97 float)
insert into testTable
select '社会单位','101',7.03,7.66 union all
select '中石化','101',7.08,7.67 union all
select '中石油','101',7.11,7.67 union all
select '社会单位','102',7.03,7.66 union all
select '中石化','102',7.08,7.67 union all
select '中石油','102',7.11,7.67
--方法一,静态SQL语句
select Province,
max(case Organization when '社会单位' then P93 else 0 end) 社会单位P93,
max(case Organization when '中石化' then P93 else 0 end) 中石化P93,
max(case Organization when '中石油' then P93 else 0 end) 中石油P93,
max(case Organization when '社会单位' then P97 else 0 end) 中石化P97,
max(case Organization when '中石化' then P97 else 0 end) 中石化P97,
max(case Organization when '中石油' then P97 else 0 end) 中石化P97
from @table group by Province
--方法二,动态SQL
declare @sqlStr varchar(8000)
set @sqlStr = 'select Province '
select @sqlStr = @sqlStr + ' , max(case Organization when ''' + Organization + ''' then P93 else 0 end) [' + Organization +'P93'+ ']'
from (select distinct Organization from testTable) as a
select @sqlStr = @sqlStr + ' , max(case Organization when ''' + Organization + ''' then P97 else 0 end) [' + Organization +'P97'+ ']'
from (select distinct Organization from testTable) as a
set @sqlStr = @sqlStr + ' from testTable group by Province'
exec(@sqlStr)
--结果
Province 社会单位P93 中石化P93 中石油P93 社会单位P97 中石化P97 中石油P97
-------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
101 7.03 7.08 7.11 7.66 7.67 7.67
102 7.03 7.08 7.11 7.66 7.67 7.67

(2 行受影响)
汤姆克鲁斯 2012-10-09
  • 打赏
  • 举报
回复
动态还是静态的?
-Tracy-McGrady- 2012-10-09
  • 打赏
  • 举报
回复
阿汤哥,求指导啊。

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧