需要帮助调整一些数据

发布于 2024-12-11 14:34:11 字数 1140 浏览 5 评论 0原文

我希望有人能帮助我。我正在尝试在 SQL Server 2005 上转换一些数据,但无法完全获得我正在寻找的结果。

这是我当前的表模式:

| ProductCode | AttributeName | AttributeValue |
| 1           | AttributeA    | 10             |
| 1           | AttributeB    | 20             |
| 2           | AttributeA    | 30             |
| 2           | AttributeB    | 40             |
| 3           | AttributeA    | 50             |

这是我想要实现的结果:

| ProductCode | AttributeA | AttributeB |
| 1           | 10         | 20         |
| 2           | 30         | 40         |
| 3           | 50         | NULL       |

我知道我可以使用以下 SQL 实现此结果:

SELECT DISTINCT ProductCode, 
    (SELECT AttributeValue 
     FROM attributes 
     WHERE ProductName = 'AttributeA' AND ProductCode=a.ProductCode) AttributeA, 
    (SELECT AttributeValue 
     FROM attributes 
     WHERE ProductName = 'AttributeB' AND ProductCode=a.ProductCode) AttributeB, 
FROM attributes a

虽然该 SQL 确实产生了我正在寻找的结果,但它显然不是动态的(在实际上,我不仅有更多的属性类型,而且不同的产品有不同的属性集)并且它还扫描了表 3 次。这也是维护的噩梦。

我尝试使用 SQL Server 的 PIVOT 功能,但没有成功。

有人可以帮忙吗?

I'm hoping someone can help me. I'm trying to pivot some data on SQL Server 2005 and can't quite get the results I'm looking for.

This is my current table schema:

| ProductCode | AttributeName | AttributeValue |
| 1           | AttributeA    | 10             |
| 1           | AttributeB    | 20             |
| 2           | AttributeA    | 30             |
| 2           | AttributeB    | 40             |
| 3           | AttributeA    | 50             |

This is the results I'm trying to achieve:

| ProductCode | AttributeA | AttributeB |
| 1           | 10         | 20         |
| 2           | 30         | 40         |
| 3           | 50         | NULL       |

I know that I can achieve this result with the following SQL:

SELECT DISTINCT ProductCode, 
    (SELECT AttributeValue 
     FROM attributes 
     WHERE ProductName = 'AttributeA' AND ProductCode=a.ProductCode) AttributeA, 
    (SELECT AttributeValue 
     FROM attributes 
     WHERE ProductName = 'AttributeB' AND ProductCode=a.ProductCode) AttributeB, 
FROM attributes a

Although that SQL does produce the result I'm looking for, it's obviously not dynamic (in reality, I not only have more Attribute Types, but different products have different sets of attributes) and it also scans the table 3 times. It's also a maintenance nightmare.

I tried using the PIVOT functionality of SQL Server, but with no luck.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2024-12-18 14:34:11
create table #attributes (ProductCode int, 
                          AttributeName varchar(20), 
                          AttributeValue int)

insert into #attributes values (1, 'AttributeA', 10)
insert into #attributes values (1, 'AttributeB', 20)
insert into #attributes values (2, 'AttributeA', 30)
insert into #attributes values (2, 'AttributeB', 40)
insert into #attributes values (3, 'AttributeA', 50)

declare @attributes_columns nvarchar(max)

set @attributes_columns
   = (
    select ', [' + AttributeName + ']'
    from 
    (
        select distinct AttributeName as AttributeName
        from #attributes
    ) t
    order by t.AttributeName
    for xml path('')
   )
set @attributes_columns = stuff(@attributes_columns,1,2,'')

declare @sql nvarchar(max) 
set @sql = N'
select ProductCode, <attributes_columns>
from
 (select ProductCode, AttributeName, AttributeValue
  from #attributes )p
 pivot 
 (
   sum(AttributeValue) for AttributeName in (<attributes_columns>)
 ) as pvt
'
set @sql = replace(@sql, '<attributes_columns>', @attributes_columns)
print @sql
exec sp_executesql @sql
drop table #attributes
create table #attributes (ProductCode int, 
                          AttributeName varchar(20), 
                          AttributeValue int)

insert into #attributes values (1, 'AttributeA', 10)
insert into #attributes values (1, 'AttributeB', 20)
insert into #attributes values (2, 'AttributeA', 30)
insert into #attributes values (2, 'AttributeB', 40)
insert into #attributes values (3, 'AttributeA', 50)

declare @attributes_columns nvarchar(max)

set @attributes_columns
   = (
    select ', [' + AttributeName + ']'
    from 
    (
        select distinct AttributeName as AttributeName
        from #attributes
    ) t
    order by t.AttributeName
    for xml path('')
   )
set @attributes_columns = stuff(@attributes_columns,1,2,'')

declare @sql nvarchar(max) 
set @sql = N'
select ProductCode, <attributes_columns>
from
 (select ProductCode, AttributeName, AttributeValue
  from #attributes )p
 pivot 
 (
   sum(AttributeValue) for AttributeName in (<attributes_columns>)
 ) as pvt
'
set @sql = replace(@sql, '<attributes_columns>', @attributes_columns)
print @sql
exec sp_executesql @sql
drop table #attributes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文