在数据透视查询中查找最后一个“非空”值

时间:2013-02-26 16:09:33

标签: php sql-server performance pivot

我有以下查询:

 SELECT * FROM 
                (
                    SELECT unix_timestamp, input_raw, tag_id 
                    from [200030].[dbo].inputs
                    WHERE inputs.date_time > dateadd(day,-1,getdate())
                    AND
                    (tag_id = 92164 or tag_id = 92149)
                ) src
                pivot
                (
                    max(input_raw)
                    FOR tag_id IN ([92164], [92149])
                ) piv
                ORDER by unix_timestamp DESC

这很棒,很有效。它给了我结果:

enter image description here

但是,我希望查询能为我做一件事。

每当存在'NULL'结果时,我希望查询将'NULL'替换为列中的最后一个'non-NULL'值。

例如,列'92164'中看到的第一个NULL将替换为'211'。

此外,有可能会有几个'NULL'一行,所以查询需要继续上升,直到找到无NULL为止。

我已经能够通过php实现这一目标。将结果放入2D关联数组并运行找到空值的函数然后循环查找最后一个非NULL,但我真的想在SQL中尽可能地执行此操作。

我更愿意使用

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
  //code     
}

方法比分配多个数组。

任何帮助?

谢谢

// EDIT

忘记添加,这仅适用于空值以上的任何非空值。例如,如果第一行为NULL,则可以接受。

2 个答案:

答案 0 :(得分:2)

您可以将您的透视查询放在CTE中,并在计算出要显示的值时重复使用CTE。您的数据仅在第一列中具有空值,但此处是处理两列中的空值的版本。​​

WITH C AS
(
  SELECT * 
  FROM (
       SELECT unix_timestamp, input_raw, tag_id 
       FROM inputs
       WHERE date_time > dateadd(day,-1,getdate()) AND
             (tag_id = 92164 OR tag_id = 92149)
       ) src
  PIVOT
       (
       MAX(input_raw)
       FOR tag_id IN ([92164], [92149])
       ) piv
)
SELECT C1.unix_timestamp,
       (
       SELECT TOP(1) C2.[92164] 
       FROM C AS C2
       WHERE C1.unix_timestamp <= C2.unix_timestamp AND
             C2.[92164] IS NOT NULL
       ORDER BY C2.unix_timestamp
       ) AS [92164],
       (
       SELECT TOP(1) C2.[92149] 
       FROM C AS C2
       WHERE C1.unix_timestamp <= C2.unix_timestamp AND
             C2.[92149] IS NOT NULL
       ORDER BY C2.unix_timestamp
       ) AS [92149] 
FROM C AS C1
ORDER by C1.unix_timestamp DESC;

如果遇到性能问题,将CTE结果存储在具有有用聚簇键索引的临时表中可能会更快。

CREATE TABLE #C
(
  unix_timestamp int PRIMARY KEY,
  [92164] int,
  [92149] int
);

INSERT INTO #C
SELECT *
FROM (
      SELECT unix_timestamp, input_raw, tag_id 
      FROM inputs
      WHERE date_time > dateadd(day,-1,getdate()) AND
            (tag_id = 92164 OR tag_id = 92149)
      ) src
PIVOT
      (
      MAX(input_raw)
      FOR tag_id IN ([92164], [92149])
      ) piv;

SELECT C1.unix_timestamp,
       (
       SELECT TOP(1) C2.[92164] 
       FROM #C AS C2
       WHERE C1.unix_timestamp <= C2.unix_timestamp AND
             C2.[92164] IS NOT NULL
       ORDER BY C2.unix_timestamp
       ) AS [92164],
       (
       SELECT TOP(1) C2.[92149] 
       FROM #C AS C2
       WHERE C1.unix_timestamp <= C2.unix_timestamp AND
             C2.[92149] IS NOT NULL
       ORDER BY C2.unix_timestamp
       ) AS [92149]
FROM #C AS C1
ORDER by C1.unix_timestamp DESC;

DROP TABLE #C;

答案 1 :(得分:0)

您可以使用&#34;运行总计&#34;计算方法detailed here。创建临时表或表变量以保存数据透视查询的结果,并使用额外的列来保存92164列的最后一个非空值。我不能让SQLFiddle这样做,但它适用于我的机器(着名的遗言)

CREATE TABLE #pivot_results ([unix_timestamp] int, [92164] int, [92149] int)
INSERT INTO #pivot_results  ([unix_timestamp], [92164], [92149])
VALUES
    (1361893407, NULL, 294),
    (1361893218, 207, 294),
    (1361893108, NULL, 292),
    (1361892807, 211, 292),
    (1361892799, NULL, 292)    
CREATE TABLE #update_me 
    ([unix_timestamp] int, [92164] int, [92149] int, last_not_null_92164 int)

DECLARE @last_not_null_92164 INT = NULL;

INSERT INTO #update_me([unix_timestamp], [92164], [92149], last_not_null_92164)
SELECT unix_timestamp, [92164], [92149], NULL
FROM #pivot_results
ORDER BY unix_timestamp DESC

UPDATE #update_me
SET @last_not_null_92164 = last_not_null_92164 = ISNULL([92164],@last_not_null_92164)
FROM #update_me

SELECT 
     unix_timestamp
    ,last_not_null_92164 AS [92164]
    ,[92149] 
FROM #update_me
ORDER BY unix_timestamp DESC