使用 SQL Server 2000 进行透视 [英] Pivot using SQL Server 2000

查看:20
本文介绍了使用 SQL Server 2000 进行透视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整理了一个我的问题的示例场景,我希望它足以让有人为我指明正确的方向.

I put together a sample scenario of my issue and I hope its enough for someone to point me in the right direction.

我有两张桌子

产品

产品元数据

我需要以下结果集

推荐答案

我们过去曾成功地使用过以下方法...

We've successfully used the following approach in the past...

SELECT [p].ProductID,
       [p].Name,
       MAX(CASE [m].MetaKey
             WHEN 'A'
               THEN [m].MetaValue
           END) AS A,
       MAX(CASE [m].MetaKey
             WHEN 'B'
               THEN [m].MetaValue
           END) AS B,
       MAX(CASE [m].MetaKey
             WHEN 'C'
               THEN [m].MetaValue
           END) AS C
FROM   Products [p]
       INNER JOIN ProductMeta [m]
         ON [p].ProductId = [m].ProductId
GROUP  BY [p].ProductID,
          [p].Name 

使用...转置聚合也很有用

It can also be useful transposing aggregations with the use of...

SUM(CASE x WHEN 'y' THEN yVal ELSE 0 END) AS SUMYVal

编辑

另外值得注意的是,这是使用 ANSI 标准 SQL,因此它可以跨平台工作 :)

Also worth noting this is using ANSI standard SQL and so it will work across platforms :)

这篇关于使用 SQL Server 2000 进行透视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆