将多行组合成一行

时间:2014-08-06 09:02:08

标签: sql sql-server

我有一个像这样的表结构: so_1.png

我需要获取每列的最新值!= NULL。我目前的方法是使用像这样的UNION语句:

SELECT TOP 1 testlong1_min, testlong1_max, NULL, NULL, NULL, NULL, NULL, NULL FROM Grenzwerte WHERE testlong1_min IS NOT NULL AND testlong1_max IS NOT NULL UNION 
SELECT TOP 1 NULL, NULL, testlong2_min, testlong2_max, NULL, NULL, NULL, NULL FROM Grenzwerte WHERE testlong2_min IS NOT NULL AND testlong2_max IS NOT NULL UNION 
SELECT TOP 1 NULL, NULL, NULL, NULL, testlong3_min, testlong3_max, NULL, NULL FROM Grenzwerte WHERE testlong3_min IS NOT NULL AND testlong3_max IS NOT NULL UNION 
SELECT TOP 1 NULL, NULL, NULL, NULL, NULL, NULL, testlong4_min, testlong4_max FROM Grenzwerte WHERE testlong4_min IS NOT NULL AND testlong4_max IS NOT NULL 

这似乎不起作用,我的结果是空的。我还考虑过每个字段执行1次查询,但我认为与UNION语句相比,开销太大了。

问题:

有没有办法构思列并在一行中返回最新值?

修改

使用Parodo的建议我现在得到这个结果,现在我需要将行合并为一个。

enter image description here

3 个答案:

答案 0 :(得分:2)

您应该使用is not null代替<> NULL,如下所示

SELECT TOP 1 testlong1_min, testlong1_max, NULL, NULL, NULL, NULL, NULL, NULL FROM Grenzwerte WHERE testlong1_min IS NOT NULL AND testlong1_max IS NOT NULL 
UNION 
SELECT TOP 1 testlong1_min, testlong1_max, NULL, NULL, NULL, NULL, NULL, NULL FROM Grenzwerte WHERE testlong1_min IS NOT NULL AND testlong1_max IS NOT NULL 
UNION 
SELECT TOP 1 NULL, NULL, testlong2_min, testlong2_max, NULL, NULL, NULL, NULL FROM Grenzwerte WHERE testlong2_min IS NOT NULL AND testlong2_max IS NOT NULL 
UNION 
SELECT TOP 1 NULL, NULL, NULL, NULL, testlong3_min, testlong3_max, NULL, NULL FROM Grenzwerte WHERE testlong3_min IS NOT NULL AND testlong3_max IS NOT NULL 
UNION 
SELECT TOP 1 NULL, NULL, NULL, NULL, NULL, NULL, testlong4_min, testlong4_max FROM Grenzwerte WHERE testlong4_min IS NOT NULL AND testlong4_max IS NOT NULL 

小心UNION,因为它可以消除重复。如果您想查看重复项,请改用UNION ALL

答案 1 :(得分:1)

在下面的声明中,每个CTE将只有所有相应的非空对,并且还将得到一个行号,其中最新的id将具有数字1.那么我只获得基于每个CTE的rownumber的最新id,所以每个只有一行。这样,CROSS JOINING将导致单行具有所需的结果。

;WITH testLong1CTE AS
(
    SELECT testlong1_min, testlong1_max, ROW_NUMBER()OVER(ORDER BY ID DESC) AS rn
    FROM   Table
    WHERE  testlong1_min IS NOT NULL AND testlong1_max IS NOT NULL
),testLong2CTE AS
(
    SELECT testlong2_min, testlong2_max, ROW_NUMBER()OVER(ORDER BY ID DESC) AS rn
    FROM   Table
    WHERE  testlong2_min IS NOT NULL AND testlong2_max IS NOT NULL
),testLong3CTE AS
(
    SELECT testlong3_min, testlong3_max, ROW_NUMBER()OVER(ORDER BY ID DESC) AS rn
    FROM   Table
    WHERE  testlong3_min IS NOT NULL AND testlong3_max IS NOT NULL
),testLong4CTE AS
(
    SELECT testlong4_min, testlong4_max, ROW_NUMBER()OVER(ORDER BY ID DESC) AS rn
    FROM   Table
    WHERE  testlong4_min IS NOT NULL AND testlong4_max IS NOT NULL
)
SELECT testlong1_min, 
       testlong1_max,
       testlong2_min, 
       testlong2_max,
       testlong3_min, 
       testlong3_max,
       testlong4_min, 
       testlong4_max
FROM (SELECT testlong1_min, testlong1_max FROM testLong1CTE WHERE rn = 1) AS T1
CROSS JOIN (SELECT testlong2_min, testlong2_max FROM testLong2CTE WHERE rn = 1) AS T2
CROSS JOIN (SELECT testlong3_min, testlong3_max FROM testLong3CTE WHERE rn = 1) AS T3
CROSS JOIN (SELECT testlong4_min, testlong4_max FROM testLong4CTE WHERE rn = 1) AS T4

答案 2 :(得分:1)

当您使用UNION运算符组合两个查询时,结果结果集是来自两个查询的串联的唯一行,这不是您想要的。如果您希望每行具有最新的非空值的单行,则应首先获取最新的非空值,并将每列连接到结果中。

; WITH
tab1 AS (
   SELECT TOP 1 testlong1_min, testlong1_max
   FROM Grenzwerte
   WHERE testlong1_min IS NOT NULL
   ORDER BY [mySequenceColumn]
),
tab2 AS (
   SELECT TOP 1 testlong2_min, testlong2_max
   FROM Grenzwerte
   WHERE testlong2_min IS NOT NULL
   ORDER BY [mySequenceColumn]
),
tab3 AS (
   SELECT TOP 1 testlong3_min, testlong3_max
   FROM Grenzwerte
   WHERE testlong3_min IS NOT NULL
   ORDER BY [mySequenceColumn]
),
tab4 AS (
   SELECT TOP 1 testlong4_min, testlong4_max
   FROM Grenzwerte
   WHERE testlong4_min IS NOT NULL
   ORDER BY [mySequenceColumn]
),
SELECT *
FROM tab1 CROSS JOIN tab2 CROSS JOIN tab3 CROSS JOIN tab4;