如何使用 SQL Server 2008 查询获得此结果?

发布于 2024-12-12 07:32:03 字数 567 浏览 0 评论 0原文

我有两个表:

Product -< Category (1 product can have many categories)

Product 表有三列:

ProductId, Name, Type
P1,Alpha Product,ClassA
P2,Beta Product,ClassB

Category 表有四列:

CategoryId, Name, Value, ProductId
C1, Category1, 0.5, P1
C2, Category2, 0.75, P1
C3, Category1, 0.25, P2
C4, Category2, 0.15, P2

我想要实现的是每个产品一行,每个类别作为一列和值作为一行例如:

ProductId, Category1, Category2
P1, 0.5, 0.75
P2, 0.25, 0.15

任何帮助表示赞赏

I have two tables:

Product -< Category (1 product can have many categories)

Product table has three columns:

ProductId, Name, Type
P1,Alpha Product,ClassA
P2,Beta Product,ClassB

Category table has four columns:

CategoryId, Name, Value, ProductId
C1, Category1, 0.5, P1
C2, Category2, 0.75, P1
C3, Category1, 0.25, P2
C4, Category2, 0.15, P2

What I want to achieve is a single row per product with each category as a column and value as a row e.g.:

ProductId, Category1, Category2
P1, 0.5, 0.75
P2, 0.25, 0.15

Any help is appreciated

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

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

发布评论

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

评论(1

夏见 2024-12-19 07:32:03

修改为演示静态和动态方法

CREATE  TABLE #t
    (
      CategoryID CHAR(2)
    , NAME VARCHAR(15)
    , VALUE DECIMAL(3, 2)
    , ProductID CHAR(2)
    )
INSERT  INTO #t
        ( CategoryID
        , NAME
        , VALUE
        , ProductID 
        )
        SELECT  'C1'
              , 'Category1'
              , 0.5
              , 'P1'
        UNION
        SELECT  'C2'
              , 'Category2'
              , 0.75
              , 'P1'
        UNION
        SELECT  'C3'
              , 'Category1'
              , 0.25
              , 'P2'
        UNION
        SELECT  'C4'
              , 'Category2'
              , 0.15
              , 'P2'
SELECT  ProductID
      , Category1
      , Category2
FROM    ( SELECT    NAME
                  , VALUE
                  , ProductID
          FROM      #t t
        ) AS Src PIVOT ( AVG(VALUE) FOR NAME IN ( [Category1], [Category2] ) ) AS p


 --dynamically

--build your columns list 
DECLARE @columns VARCHAR(MAX)
SELECT  @columns = STUFF(( SELECT DISTINCT
                                    ',' + NAME
                           FROM     #t t
                           ORDER BY ',' + NAME
                         FOR
                           XML PATH('')
                         ), 1, 1, '')    

--build your sql statement 
DECLARE @sql VARCHAR(MAX)
SET @sql = REPLACE('SELECT  ProductID
      , |@columns|
FROM    ( SELECT    NAME
                  , VALUE
                  , ProductID
          FROM      #t t
        ) AS Src PIVOT ( AVG(VALUE) FOR NAME IN ( |@columns| ) ) AS p', '|@columns|', @columns)

 EXEC(@sql)

Modified to demonstrate both static and dynamic methods

CREATE  TABLE #t
    (
      CategoryID CHAR(2)
    , NAME VARCHAR(15)
    , VALUE DECIMAL(3, 2)
    , ProductID CHAR(2)
    )
INSERT  INTO #t
        ( CategoryID
        , NAME
        , VALUE
        , ProductID 
        )
        SELECT  'C1'
              , 'Category1'
              , 0.5
              , 'P1'
        UNION
        SELECT  'C2'
              , 'Category2'
              , 0.75
              , 'P1'
        UNION
        SELECT  'C3'
              , 'Category1'
              , 0.25
              , 'P2'
        UNION
        SELECT  'C4'
              , 'Category2'
              , 0.15
              , 'P2'
SELECT  ProductID
      , Category1
      , Category2
FROM    ( SELECT    NAME
                  , VALUE
                  , ProductID
          FROM      #t t
        ) AS Src PIVOT ( AVG(VALUE) FOR NAME IN ( [Category1], [Category2] ) ) AS p


 --dynamically

--build your columns list 
DECLARE @columns VARCHAR(MAX)
SELECT  @columns = STUFF(( SELECT DISTINCT
                                    ',' + NAME
                           FROM     #t t
                           ORDER BY ',' + NAME
                         FOR
                           XML PATH('')
                         ), 1, 1, '')    

--build your sql statement 
DECLARE @sql VARCHAR(MAX)
SET @sql = REPLACE('SELECT  ProductID
      , |@columns|
FROM    ( SELECT    NAME
                  , VALUE
                  , ProductID
          FROM      #t t
        ) AS Src PIVOT ( AVG(VALUE) FOR NAME IN ( |@columns| ) ) AS p', '|@columns|', @columns)

 EXEC(@sql)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文