SQL - 每行一项

时间:2015-09-16 12:52:36

标签: sql unpivot

我有一个名为Registers的表,包含3个字段,如下所示:

Select PersonID, RegisterID, Status from Registers

PersonID    RegisterID  Status
-------------------------------
3049518     14467       A
3049518     14467       B
3049518     14467       C
3049518     32445       A
3049518     32445       B
3049518     32445       C

是否有一种简单的方法可以在单行显示每个寄存器及其状态?例如

PersonID    RegisterID  Status1 Status2 Status3
------------------------------------------------
3049518     14467       A       B       C
3049518     32445       A       B       C

提前致谢。

3 个答案:

答案 0 :(得分:0)

你可以做这样的事情

  SELECT SUBSTRING((SELECT   ',' + CONVERT(VARCHAR , Status)       
                                                            FROM DBO.Registers WITH (NOLOCK)

                                                            FOR XML PATH('')),2,20000)),'')

或者提供sql小提琴,以便我编写完整的查询

答案 1 :(得分:0)

如果您拥有固定数量的状态并且您知道状态值,那么您可以在使用以下代码后获得您的状态。

CREATE TABLE #Register(PersonID int, RegisterID int, Stat varchar)

INSERT INTO #Register (PersonID,RegisterID,Stat) VALUES
('3049518','14467','A'),
('3049518','14467','B'),
('3049518','14467','C'),
('3049518','32445','A'),
('3049518','32445','B'),
('3049518','32445','C')

SELECT PersonID
    ,RegisterID
    ,MAX(A) 'A'
    ,MAX(B) 'B'
    ,MAX(C) 'C'
FROM (
    SELECT PersonID
        ,RegisterID
        ,CAST(CASE 
                WHEN r.stat = 'A'
                    THEN 'A'
                ELSE NULL
                END AS VARCHAR) AS 'A'
        ,CAST(CASE 
                WHEN r.stat = 'B'
                    THEN 'B'
                ELSE NULL
                END AS VARCHAR) AS 'B'
        ,CAST(CASE 
                WHEN r.stat = 'C'
                    THEN 'C'
                ELSE NULL
                END AS VARCHAR) AS 'C'
    FROM #Register r
    GROUP BY PersonID
        ,RegisterID
        ,Stat
    ) q
GROUP BY q.PersonID
    ,q.RegisterID

该代码假定每个人每个状态有一条记录。

输出

code result

如果状态值未修复且您不知道它们是什么,则可以在SQL Server中使用以下内容:

CREATE TABLE #Register(PersonID int, RegisterID int, Stat varchar)

INSERT INTO #Register (PersonID,RegisterID,Stat) VALUES
('3049518','14467','A'),
('3049518','14467','B'),
('3049518','14467','C'),
('3049518','32445','A'),
('3049518','32445','B'),
('3049518','32445','C')


DECLARE @queryPart1 NVARCHAR(MAX)
    ,@queryPart2 NVARCHAR(MAX)
    ,@colHeader NVARCHAR(MAX)
    ,@query NVARCHAR(MAX)

SET @queryPart1 = 'SELECT PersonID,RegisterID'
SET @queryPart2 = 'FROM (SELECT PersonID,RegisterID'
SET @query = ''

DECLARE Stat_Cursor CURSOR
FOR
SELECT DISTINCT (STAT)
FROM #Register r

OPEN Stat_Cursor;

FETCH NEXT
FROM Stat_Cursor
INTO @colHeader

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @queryPart1 = @queryPart1 + ',MAX(''' + @colHeader + ''') ''' + @colHeader + ''''
    SET @queryPart2 = @queryPart2 + ',CAST(CASE WHEN r.stat = ''' + @colHeader + ''' THEN ''' + @colHeader + ''' ELSE NULL END AS VARCHAR) AS ''' + @colHeader + ''''

    FETCH NEXT
    FROM Stat_Cursor
    INTO @colHeader
END;

CLOSE Stat_Cursor;

DEALLOCATE Stat_Cursor;

SET @query = @queryPart1 + @queryPart2 + 'FROM #Register r GROUP BY PersonID ,RegisterID,Stat) q'

PRINT @query
GO

答案 2 :(得分:0)

如果您使用Oracle,这是一个解决方案:

    select * from ( 
    select PersonID,RegisterID, Status
   from test t
)
pivot 
(
   count(Status)
   for Status in ('A' as "Status1",'B' "Status2",'C' "Status3")
)
order by 1

SQLFiddle