将多行移动到具有Concate情境的列

时间:2018-03-23 10:04:23

标签: sql sql-server pivot concatenation

我可能认为这是一个简单的问题 - 我需要根据成员ID和日期的串联将多行移动到一行中。

我所拥有的是一张可能如下所示的表:

Row   Member ID      Create Date    Answer
1     1234          20170101    20170101
2     1234          20170101    1
3     1234          20170201    20170201
4     7778          20180301    1
5     7781          20180201    2080201
6     7778          20180301    20180301

我需要的是在可能的Concate情况下将成员ID与创建日期匹配,然后将该成员/创建日期的两个答案移动到一行。

最终结果如下:

Row    Member ID    Create Date Answer     Answer 2
1      1234         20170101    20170101    1
2      1234         20170201    20170201    
3      7778         20180301    1      20180301
4      7781         20180201    2080201 

3 个答案:

答案 0 :(得分:0)

如果您知道最多有两个答案,那么您可以这样做:

select row_number() over (order by min(id)) as new_id,
       MemberID, Create Date, min(Answer) as Answer,
       (case when max(answer) <> min(Answer) then max(answer) end) as answer2
from t
group by MemberID, Create Date

答案 1 :(得分:0)

在SQL Server 2017中,您可以使用composer dump-autoload并将结果汇​​总到一列:

STRING_AGG

http://sqlfiddle.com/#!18/72775/3

答案 2 :(得分:0)

对于任意数量的答案栏

Select
    Row_NUmber() over(order by (select 1)) as id,
    A.[Member ID],A.[Create Date]
    ,STUFF((
          SELECT ',' +  CAST([Answer] AS varchar)
          FROM Table1 T
          WHERE A.[Member ID]=T.[Member ID] and A.[Create Date]=T.[Create Date] 
          FOR XML PATH('')), 1, 1, '') as [Answer] 
         into #test
FROM Table1 A
Group by  A.[Member ID],A.[Create Date]
DECLARE @pivot varchar(8000)
DECLARE @select varchar(8000)
SELECT 
        @pivot=coalesce(@pivot+',','')+'[Answer'+cast(number+1 as varchar(10))+']'
FROM 
        master..spt_values where type='p' and 
        number<=(SELECT max(len(Answer)-len(replace(Answer,',',''))) FROM #test)

SELECT 
    @select='
        select p.*
        from (
        select 
            id, [Member ID],[Create Date],substring(Answer, start+2, endPos-Start-2) as token,
            ''Answer''+cast(row_number() over(partition by id order by start) as varchar(10)) as n
        from (
            select 
                id, [Member ID],[Create Date], Answer, n as start, charindex('','',Answer,n+2) endPos
                from (select number as n from master..spt_values where type=''p'') num
                cross join 
                (
                    select 
                        id,[Member ID],[Create Date], '','' + Answer +'','' as Answer 
                    from 
                        #test
                ) m
            where n < len(Answer)-1
            and substring(Answer,n+1,1) = '','') as Answer
        ) pvt
        Pivot ( max(token)for n in ('+@pivot+'))p order by id'

EXEC(@select)

输出:

id  Member ID   Create Date Answer1 Answer2
1   1234    20170101    20170101    1
2   1234    20170201    20170201    null
3   7778    20180301    1           20180301
4   7781    20180201    2080201     null

现场演示

  

http://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=f8bc3689108b8cec2c95259182296f76