【发布时间】:2010-05-13 20:38:22
【问题描述】:
我有一个表格(实际上是一个视图,但将我的示例简化为一个表格),它为我提供了一些这样的数据
CompanyName website
Google google.com
Google google.net
Google google.org
Google google.in
Google google.de
Microsoft Microsoft.com
Microsoft live.com
Microsoft bing.com
Microsoft hotmail.com
我希望将其转换为这样的结果
CompanyName website1 website2 website3 website 4 website5 website6
----------- ------------- ---------- ---------- ----------- --------- --------
Google google.com google.net google.org google.in google.de NULL
Microsoft Microsoft.com live.com bing.com hotmail.com NULL NULL
我已经研究过数据透视,但看起来记录(行值)不能是动态的(即只能是某些预定义的值)。
另外,如果网站超过6个,我想限制在前6个
动态枢轴是有道理的,但我必须将它纳入我的观点??有没有更简单的解决方案?
这里是 SQL 脚本
CREATE TABLE [dbo].[Company](
[CompanyName] [varchar](50) NULL,
[website] [varchar](50) NULL
) ON [PRIMARY]
GO
insert into company values ('Google','google.com')
insert into company values ('Google','google.net')
insert into company values ('Google','google.org')
insert into company values ('Google','google.in')
insert into company values ('Google','google.de')
insert into company values ('Microsoft','Microsoft.com')
insert into company values ('Microsoft','live.com')
insert into company values ('Microsoft','bing.com')
insert into company values ('Microsoft','hotmail.com')
编辑:我正在删除 ID,因为它是为简化而创建的。我想我不应该拥有它。对不起
【问题讨论】:
-
你能说一下你为什么要这样吗?它是用于演示还是供其他程序使用?
-
这是供其他程序使用的
标签: sql-server pivot