簡體   English   中英

將多行合並為一行

[英]Combining multiple rows into one single row

我有一個這樣的表結構: 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的建議,我現在得到了這個結果,現在我需要將行合並為一個。

在此處輸入圖片說明

您應該使用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

在下面的語句中,每個CTE僅具有所有各自的非null對,並且還將獲得一個行號,其中最新ID的編號為1。因此,我僅基於每個CTE的行號獲得最新的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

當您使用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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM