简体   繁体   English

如何将SQL Server行数据转换为列?

[英]How to transform SQL Server row data to columns?

I queried data like this 我这样查询数据

member_no cover_version product_id product_name         product_type
--------- ------------- ---------- -------------------- ------------
11421     7                      4 Excellent More       E           
11421     7                     15 Comprehensive Data   D           

But I want to shape this data like this: 但是我想像这样塑造这些数据:

member_no cover_version product_e_id product_e_name       product_d_id product_d_name
--------- ------------- ------------ -------------------- ------------ --------------------
11421     7                        4 Excellent More                 15 Comprehensive Data  

I am using SQL Server 2008. What should be the best approach to shape the data as I want? 我正在使用SQL Server2008。根据需要,什么是最好的数据整形方法?

Assuming you've only got product types D and E as stated, a simple self-join will get you what you're after. 假设您只获得了所述的D和E产品类型,那么简单的自联接将为您提供所需的服务。

If you want something more generic, please expand your question. 如果您想要更通用的东西,请扩展您的问题。

DROP TABLE IF EXISTS #Demo

SELECT
    *
INTO
    #Demo
FROM
    (VALUES 
    (11421, 7, 4, 'Excellent More', 'E')
   ,(11421, 7, 15, 'Comprehensive Data', 'D'))  A
    (member_no, cover_version, product_id, product_name, product_type) 

SELECT
    D.member_no
   ,D.cover_version
   ,E.product_id product_e_id
   ,E.product_name product_e_name
   ,D.product_id product_d_id
   ,D.product_name product_d_name
FROM
    #Demo D
JOIN #Demo E ON D.member_no = E.member_no
                AND D.product_type = 'D'
                AND E.product_type = 'E';


member_no   cover_version product_e_id product_e_name     product_d_id product_d_name
----------- ------------- ------------ ------------------ ------------ ------------------
11421       7             4            Excellent More     15           Comprehensive Data

If you want to do this dynamically, based on unknown product types, you can use this. 如果要基于未知的产品类型动态地执行此操作,则可以使用此功能。

DECLARE @SQL NVARCHAR(MAX),
        @Columns NVARCHAR(MAX),
        @CaseExpressions NVARCHAR(MAX) = 'MAX(CASE WHEN product_type = ''<<productType>>'' THEN product_id END) AS [product_<<productType>>_id],
                                          MAX(CASE WHEN product_type = ''<<productType>>'' THEN product_name END) AS [product_<<productType>>_name]'

-- build concatenated string with 2 columns for each product_type in your table.
-- group by product_type to get distinct results.
-- replaces <<productType>> with the actual product_type value
SELECT  @Columns = COALESCE(@Columns + ',', '') + REPLACE(@CaseExpressions, '<<productType>>', product_type)
FROM    myTable
GROUP BY product_type

-- build select query
SET     @SQL = 'SELECT  member_no, cover_version,' + @Columns + ' FROM myTable GROUP BY member_no, cover_version'

-- to see what the dynamic sql looks like
PRINT   @SQL 

-- to execute the dynamic sql and see result
EXEC    (@SQL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM