将行数据转换为SQL Server中的列

时间:2015-11-27 06:37:17

标签: sql sql-server multiple-columns rows

我有一个场景,其中我有三列多行,我需要在单行中选择它们。 下面我附上临时表格场景。

CREATE TABLE #Temp
(
  PersonID INT ,
  PhoneType VARCHAR(20) ,
  PhoneNumber VARCHAR(20)
)

INSERT  INTO #Temp
    ( PersonID ,
      PhoneType ,
      PhoneNumber 
    )
    SELECT  1212 ,
            'Business' ,
            '123456789'
    UNION ALL
    SELECT  1212 ,
            'Cell' ,
            '741852963'
    UNION ALL
    SELECT  1212 ,
            'Other' ,
            '987654321'
    UNION ALL
    SELECT  1212 ,
            'Home' ,
            '951357852'

SELECT  *
FROM    #Temp

DROP TABLE #Temp

我需要显示,

PersonID|Business|123456789|Cell|741852963|Other|987654321|Home|951357852

有人可以帮忙吗?

我必须将此结果与另一个Select Query相结合,我该怎么办?

5 个答案:

答案 0 :(得分:6)

你可以像下面这样使用PIVOT,

SELECT  *
FROM    #Temp
PIVOT(MAX(PhoneNumber) 
          FOR PhoneType IN ([Business],[Cell],[Other],[Home])) AS PVTTable

或者我认为你想要下面的内容,

SELECT PersonID = STUFF((SELECT  PhoneType + ' | ' + PhoneNumber + ' | '
    FROM #Temp 
     ORDER BY PersonID
     FOR XML PATH('')), 1, 0, '')
FROM #Temp AS x
GROUP BY PersonID, PhoneType
ORDER BY PersonID;

最后一个,

DECLARE @MainColumn AS NVARCHAR(MAX) = ''
SET @MainColumn = (SELECT  PersonID =  STUFF((SELECT PhoneType + ' | ' + PhoneNumber + ' | '
    FROM #Temp 
     ORDER BY PersonID
     FOR XML PATH('')), 1, 0, '')
FROM #Temp AS x
GROUP BY PersonID)

SELECT 'PersonID | ' + @MainColumn  AS FinalResult

答案 1 :(得分:4)

您可以使用条件聚合:

SQL Fiddle

<div class="bg-transparent"></div>

如果您的SELECT PersonID, Business = MAX(CASE WHEN PhoneType = 'Business' THEN PhoneNumber END), Cell = MAX(CASE WHEN PhoneType = 'Cell' THEN PhoneNumber END), Other = MAX(CASE WHEN PhoneType = 'Other' THEN PhoneNumber END), Home = MAX(CASE WHEN PhoneType = 'Home' THEN PhoneNumber END) FROM #Temp GROUP BY PersonID 数量未知,请执行动态交叉表:

SQL Fiddle

PhoneType

答案 2 :(得分:4)

您可以使用此代码。它可能会帮助你。

DECLARE @cols AS NVARCHAR(MAX),@query  AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(PhoneType) 
                    from #Temp
                    group by PhoneType, PersonID
                    order by PersonID
             FOR XML PATH('')), 1, 1, '')

SET @query = N'SELECT ' + @cols + N' from 
             (
                select *
                from #Temp
            ) x
            pivot 
            (
                max(PhoneNumber)
                for PhoneType in (' + @cols + N')
            ) p'

EXEC SP_EXECUTESQL @QUERY;

答案 3 :(得分:2)

试试这个

DECLARE @String VARCHAR(MAX) --To store the result
--Make the result by appending each rows
SELECT  @String = ISNULL(@String,'PersonID')  + ' | '+ PhoneType + ' | ' + PhoneNumber
FROM    #Temp
--return the string
SELECT @String

答案 4 :(得分:2)

我的解决方案是:

SELECT outerTbl.MyDisplayValue
FROM
(
    SELECT  DISTINCT CAST(PersonID AS varchar(MAX)) + Concatenated.Contacts
    FROM #Temp AS tbl
    OUTER APPLY
    (
        SELECT '|' + innerTbl.PhoneType + '|' + innerTbl.PhoneNumber
        FROM #Temp AS innerTbl  
        WHERE innerTbl.PersonID=tbl.PersonID
        FOR XML PATH('')
    ) AS Concatenated(Contacts)
    FOR XML PATH('')
) AS outerTbl(MyDisplayValue)