如何在SQL Server 2008中从resultset值创建临时表
我要做的是在SQLServer2008中创建一个临时表,它使用resultset中的列名 例如:这是我从结果集中得到的结果:如何在SQL Server 2008中从resultset值创建临时表,sql,sql-server-2008,Sql,Sql Server 2008,我要做的是在SQLServer2008中创建一个临时表,它使用resultset中的列名 例如:这是我从结果集中得到的结果: Account weight zone 22 5 1 23 3 2 22 5 1 23 3 2 24 7 3 从该结果集中,区域列值应基于区域计数转换为动态列,例如 Account weight zone 1 zone 2 zone 3 22
Account weight zone
22 5 1
23 3 2
22 5 1
23 3 2
24 7 3
从该结果集中,区域
列值应基于区域计数转换为动态列,例如
Account weight zone 1 zone 2 zone 3
22 5 2
23 3 2
24 7 1
请帮助我?您可以使用:
select account, weight
sum(case when zone = 1 then 1 end) as zone1,
sum(case when zone = 2 then 1 end) as zone2,
sum(case when zone = 3 then 1 end) as zone3
from your_table
group by account, weight
看。
另外,您还可以发现一个有趣的问题:您可以通过使用动态sql查询来解决这个问题。浏览一些关于动态旋转的非常好的帖子
我猜分区的值将比1、2、3更可变。是的,分区值范围约为1到500
SELECT Account, Weight, [1] AS Zone1, [2] AS Zone2, [3] AS Zone3, [4] AS Zone4
FROM AccountWeight
PIVOT
(
COUNT(Zone) FOR Zone IN ([1], [2], [3], [4])
) AS ResultTable
ORDER BY Account