【问题标题】:Unpivoting with Column headers - single row使用列标题取消透视 - 单行
【发布时间】:2019-02-18 12:49:49
【问题描述】:

我有一些关于某个特定人的信息,我想在两行而不是多列中显示。

我希望列是“ColumnName”和“ColumnData”

这是我返回单行的查询:

SELECT C.CUSTOMER_NAME
      ,CC.CONTACT_NAME
      ,CC.TELEPHONE
      ,CC.FAX
      ,CC.CONTACT_INITIALS
      ,CC.CONTACT_FIRSTNAME
      ,CC.EMAIL
      ,CC.CONTACT_DEAR
      ,CC.NUMERIC_PHONE_NO
      ,CC.TELEPHONE_NUMBER2
      ,CC.MOBILE_TELEPHONE
      ,CC.NUMERIC_TELEPHONE2
      ,CC.NUMERIC_MOBILE
      ,CC.NUMERIC_FAX
      ,CC.CONTACT_FULL_NAME
      ,CONTACT_MIDDLE_NAMES
FROM table C 
INNER JOIN table CC
ON C.column = CC.column
WHERE C.column = @CustomerAccount

我已尝试对此进行反透视,但由于没有聚合且每行只有一个值,因此未能使其正常工作。

虽然我可以从 sys.columns 中获取列名,但我无法将它们与表相关联,并且还必须取消透视它们。

有没有办法将这一行变成两列,包括列名和该列中的数据?

任何帮助、链接或指导将不胜感激。

谢谢

会。

【问题讨论】:

  • 编辑问题添加一些示例数据和所需的结果会有所帮助。

标签: sql-server tsql unpivot


【解决方案1】:

您可以使用UNPIVOT,如下查询。

;WITH cte 
     AS (SELECT C.customer_name, 
                CC.contact_name, 
                CC.telephone, 
                CC.fax, 
                CC.contact_initials, 
                CC.contact_firstname, 
                CC.email, 
                CC.contact_dear, 
                CC.numeric_phone_no, 
                CC.telephone_number2, 
                CC.mobile_telephone, 
                CC.numeric_telephone2, 
                CC.numeric_mobile, 
                CC.numeric_fax, 
                CC.contact_full_name, 
                contact_middle_names 
         FROM   table C 
                INNER JOIN table CC 
                        ON C.COLUMN = CC.COLUMN 
         WHERE  C.COLUMN = @CustomerAccount) 
SELECT u.x AS ColumnName, 
       u.y AS ColumnValue 
FROM   cte s 
       UNPIVOT ( [y] 
               FOR [x] IN (customer_name, 
                           contact_name, 
                           telephone, 
                           fax, 
                           contact_initials, 
                           contact_firstname, 
                           email, 
                           contact_dear, 
                           numeric_phone_no, 
                           telephone_number2, 
                           numeric_mobile, 
                           numeric_fax, 
                           contact_full_name, 
                           contact_middle_names) ) u; 

Online Demo

【讨论】:

    【解决方案2】:

    这是一种无需实际使用动态 SQL 即可“动态”取消透视几乎任何表、视图或临时查询的技术

    显然 UNPIVOT 会更高效,但您不必在此处指定列名。

    示例

    Select C.*
     From  ( Your table or Query Here ) A
     Cross Apply ( values (cast((Select A.* for XML RAW) as xml))) B(XMLData)
     Cross Apply (
                   Select Item  = xAttr.value('local-name(.)', 'varchar(100)')
                         ,Value = xAttr.value('.','varchar(max)')
                    From  XMLData.nodes('//@*') xNode(xAttr)
                    Where xAttr.value('local-name(.)','varchar(100)') not in ('Columns','ToExclude')
                 ) C
    

    我应该注意WHERE 是可选的。

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 2016-07-06
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多