7

我有以下查询:

 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

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

在此处输入图像描述

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

每当有“NULL”结果时,我希望查询用列中最后一个“非NULL”值替换“NULL”。

例如,在列“92164”中看到的第一个 NULL 将被替换为“211”。

此外,可能会有几个 'NULL' 的 ia 行,因此查询需要继续向上列,直到找到一个无 NULL。

我已经能够用 php 完成这个任务。将结果放入二维关联数组并运行一个查找空值的函数,然后循环查找最后一个非空值,但如果可能的话,我真的想在 SQL 中完成这一切。我宁愿使用

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

方法比分配几个数组。

有什么帮助吗?

谢谢

//编辑

忘了补充一点,这仅适用于空值上方有任何非空值的情况。例如,如果第一行为 NULL,则可以接受。

4

2 回答 2

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;
于 2013-02-26T19:01:55.360 回答
0

您可以使用此处详述的“运行总计”计算方法的改编版。创建一个临时表或表变量来保存数据透视查询的结果,并使用一个额外的列来保存 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
于 2013-02-26T19:10:27.643 回答