我正在使用HANA SQL脚本,并且在计算之后,我在视图/表中的数据显示如下
Col A Col B Col C
1000042965 20090830 15.9
1000042965 20090906 0
1000042965 20090913 0
1000042965 20090920 14.2
1000042965 20090927 0
1000042965 20091004 0
1000042965 20091011 14.4
1000042967 20090830 13.2
1000042967 20090906 0
1000042967 20090913 12.9
1000042967 20090920 0
1000042967 20090927 0
1000042967 20091004 11.2
1000042967 20091011 11.2
和我的预期输出为
Col A Col B Excepted Result for Col C
1000042965 20090830 15.9
1000042965 20090906 15.9
1000042965 20090913 15.9
1000042965 20090920 14.2
1000042965 20090927 14.2
1000042965 20091004 14.2
1000042965 20091011 14.4
1000042967 20090830 13.2
1000042967 20090906 13.2
1000042967 20090913 12.9
1000042967 20090920 12.9
1000042967 20090927 12.9
1000042967 20091004 11.2
1000042967 20091011 11.2
我只想返回先前的not null值。我尝试使用Windows LAG 功能,但我想要的输出不如上所述。
LAG(col C) OVER (partition by Col A)
我引用了下面的链接并尝试实现相同的逻辑,但我无法得到 Finding the previous non-zero value from an date-ordered list
SELECT distinct col A, Col B,
CASE WHEN col C = 0 THEN
IFNULL(( SELECT col C
FROM TABLE as T2
WHERE CAST(T2.col B AS DATE) < T1.col B AND T1.Col A = T2.col A AND T2.Col C > 0
),0)
ELSE Col C END Col C
FROM TABLE as T1
答案 0 :(得分:1)
我的猜测是问题是子查询没有order by
或limit
(我认为Hana SQL使用limit
),所以它可以返回多行。
查询本身很难阅读。你不应该在列名中有空格,所以像这样的东西可能有效:
SELECT colA, ColB,
(CASE WHEN colC = 0
THEN IFNULL((SELECT colC
FROM TABLE T2
WHERE T1.ColA = T2.colA AND
T2.colB < T1.colB AND
T2.ColC > 0
ORDER BY T2.COLB DESC
LIMIT 1
), 0)
ELSE ColC
END) as ColC
FROM TABLE T1