这是related previous question的扩展版本。我已经为Erwin Brandstetter发布了一个新问题,建议我这样做。 (在我回答第一个问题之后,我意识到我真的想要这个)
拥有以下数据(空白表示NULL):
ID User ColA ColB ColC
1 1 15 20
2 1 11 4
3 1 3
4 2 5 5 10
5 2 6
6 2 8
7 1 1
如何为所有用户获取每列的最后一个非NULL值,这是最简单的方法吗?因此,给定数据的结果将是:
User ColA ColB ColC
1 11 1 20
2 6 8 10
我找不到多少,似乎与我所描述的类似的函数是COALESCE
,但在我的情况下它没有按预期工作。
注意:标准SQL(如果可能),否则为PostgreSQL。所涉及列的计数可能会发生变化,因此与这三个特定列无关的解决方案将是最佳的。
答案 0 :(得分:2)
与我在上一个问题上发布的内容类似,recursive CTE非常优雅,可能是在标准SQL 中执行此操作的最快方法 - 特别是对于每个用户的许多行。
WITH RECURSIVE t AS (
SELECT row_number() OVER (PARTITION BY usr ORDER BY id DESC) AS rn
,usr, cola, colb, colc
FROM tbl
)
, x AS (
SELECT rn, usr, cola, colb, colc
FROM t
WHERE rn = 1
UNION ALL
SELECT t.rn, t.usr
, COALESCE(x.cola, t.cola)
, COALESCE(x.colb, t.colb)
, COALESCE(x.colc, t.colc)
FROM x
JOIN t USING (usr)
WHERE t.rn = x.rn + 1
AND (x.cola IS NULL OR x.colb IS NULL OR x.colc IS NULL)
)
SELECT DISTINCT ON (usr)
usr, cola, colb, colc
FROM x
ORDER BY usr, rn DESC;
-> sqlfiddle for requested PostgreSQL.
唯一的非标准元素是DISTINCT ON
,它是标准中DISTINCT
的扩展名。将最终SELECT
替换为标准SQL:
SELECT usr
,max(cola) As cola
,max(colb) As colb
,max(colc) As colc
FROM x
GROUP BY usr
ORDER BY usr;
“标准SQL”请求的用途有限。该标准仅存在于纸面上。没有RDBMS实现100%标准的SQL - 它也是毫无意义的,因为标准包括无意义的部分。可以说,PostgreSQL的实现是最接近标准的。
此解决方案特定于PostgreSQL,但应该表现得非常好。
我正在建造在上面小提琴演示的同一张桌子上。
CREATE OR REPLACE FUNCTION f_last_nonull_per_user()
RETURNS SETOF tbl AS
$func$
DECLARE
_row tbl; -- table name can be used as row type
_new tbl;
BEGIN
FOR _new IN
SELECT * FROM tbl ORDER BY usr, id DESC
LOOP
IF _new.usr = _row.usr THEN
_row.id := _new.id; -- copy only id
IF _row.cola IS NULL AND _new.cola IS NOT NULL THEN
_row.cola := _new.cola; END IF; -- only if no value found yet
IF _row.colb IS NULL AND _new.colb IS NOT NULL THEN
_row.colb := _new.colb; END IF;
IF _row.colc IS NULL AND _new.colc IS NOT NULL THEN
_row.colc := _new.colc; END IF;
ELSE
IF _new.usr <> _row.usr THEN -- doesn't fire on first row
RETURN NEXT _row;
END IF;
_row := _new; -- remember row for next iteration
END IF;
END LOOP;
RETURN NEXT _row; -- return row for last usr
END
$func$ LANGUAGE plpgsql;
致电:
SELECT * FROM f_last_nonull_per_user();
返回整行 - 包括我们需要填充所有列的最小id
。
答案 1 :(得分:1)
此查询很容易转换为MS SQL。如果您需要更多具体的内容添加评论。 Mysql查询:
<强> SQLFIDDLEExample 强>
SELECT
t1.User,
(SELECT ColA
FROM Table1
WHERE ColA is not null
AND Table1.User = t1.User
ORDER BY ID DESC
LIMIT 1 ) as ColA,
(SELECT ColB
FROM Table1
WHERE ColB is not null
AND Table1.User = t1.User
ORDER BY ID DESC
LIMIT 1 ) as ColB,
(SELECT ColC
FROM Table1
WHERE ColC is not null
AND Table1.User = t1.User
ORDER BY ID DESC
LIMIT 1 ) as ColC
FROM Table1 t1
GROUP BY t1.User
结果:
| USER | COLA | COLB | COLC |
-----------------------------
| 1 | 11 | 1 | 20 |
| 2 | 6 | 8 | 10 |