SQL Server如何在某些列为空时对行进行分组

时间:2018-02-06 00:32:19

标签: sql-server

我想知道如何在列为空时对SQL Server中的行进行分组,例如。

ID   |col1 |col2      |col3|col4 |col5      |col6|col4 |col5      |col6|col7      |
-----|-----|----------|----|-----|----------|----|-----|----------|----|----------|
1256 |2    |2018-01-01|CP  |     |          |    |     |          |    |2018-01-01|
1256 |     |          |    |3    |2018-01-02|CR  |     |          |    |2018-01-02|
1256 |     |          |    |     |          |    |4    |2018-01-03|CA  |2018-01-03|

结果就是这个

ID   |col1 |col2      |col3|col4 |col5      |col6|col4 |col5      |col6|

-----|-----|----------|----|-----|----------|----|-----|----------|----|
1256 |2    |2018-01-01|CP  |3    |2018-01-02|CR  |4    |2018-01-03|CA  |

2 个答案:

答案 0 :(得分:0)

您可以像follwong一样编写查询(样本中只包含少量列)。它会给你想要的输出。

    WITH MyTable as (
    select * from (values 
        (1256 , 2 ,   getdate(),'CP',NULL,CAST(GETDATE() AS DATE))
        ,(1256,  null ,   null,NULL,3,CAST(GETDATE() AS DATE))
        ,(1256,  null ,   null,NULL,NULL,CAST(GETDATE() AS DATE))

    ) t (Id, Col1  , Col2,Col3,Col4,Col7)
  )


 SELECT DISTINCT id, 
            C1.col1, 
            C2.col2, 
            C3.col3, 
            C4.col4 
FROM   mytable MT 
   OUTER apply (SELECT TOP 1 col1 
                FROM   mytable I 
                WHERE  I.id = MT.id 
                       AND col1 IS NOT NULL)C1 
   OUTER apply (SELECT TOP 1 col2 
                FROM   mytable I 
                WHERE  I.id = MT.id 
                       AND col2 IS NOT NULL)C2 
   OUTER apply (SELECT TOP 1 col3 
                FROM   mytable I 
                WHERE  I.id = MT.id 
                       AND col3 IS NOT NULL)C3 
   OUTER apply (SELECT TOP 1 col4 
                FROM   mytable I 
                WHERE  I.id = MT.id 
                       AND col4 IS NOT NULL)C4 

另一种方法

WITH MyTable as (
    select * from (values 
        (1256 , 2 ,   getdate(),'CP',NULL,CAST(GETDATE() AS DATE))
        ,(1256,  null ,   null,NULL,3,CAST(GETDATE() AS DATE))
        ,(1256,  null ,   null,NULL,NULL,CAST(GETDATE() AS DATE))

    ) t (Id, Col1  , Col2,Col3,Col4,Col7)
)


SELECT DISTINCT ID 
,(SELECT TOP 1 Col1 from MyTable Where Id = MT.Id AND Col1 IS NOT NULL ) AS Col1
,(SELECT TOP 1 Col2 from MyTable Where Id = MT.Id AND Col2 IS NOT NULL ) AS Col2
,(SELECT TOP 1 Col3 from MyTable Where Id = MT.Id AND Col3 IS NOT NULL ) AS Col3
,(SELECT TOP 1 Col4 from MyTable Where Id = MT.Id AND Col4 IS NOT NULL ) AS Col4

FROM MyTable MT

<强>输出

ID     Col1 COL2                     Col3     Col4
1256    2   2018-02-06 07:26:49.260  CP        3

答案 1 :(得分:0)

好的,它可以在子查询上聚合最大/最小值,我必须找到的方式是(使用由@PSK创建的查询)

SELECT DISTINCT ID 
,max(MT.col1) AS Col1
,max(MT.col2) AS Col2
,max(MT.col3) AS Col3
,max(MT.col4) AS Col4

FROM 
(
    select * from (values 
        (1256 , 2 ,   getdate(),'CP',NULL,CAST(GETDATE() AS DATE))
        ,(1256,  null ,   null,NULL,3,CAST(GETDATE() AS DATE))
        ,(1256,  null ,   null,NULL,NULL,CAST(GETDATE() AS DATE))

    ) t (Id, Col1  , Col2,Col3,Col4,Col7)
  ) AS MT
GROUP by
ID

和结果相同