什么 SQL 查询可以满足所有这些要求?我需要它来实现和测试窗口聚合函数。
对于此示例,预期结果是:
'aaa,bbb,ccc'
。这些是我的尝试,没有一个满足上述要求。
-- preload data
CREATE TABLE tbl(a INTEGER PRIMARY KEY, b TEXT);
INSERT INTO tbl VALUES (1, 'bbb'), (2, 'ccc'), (3, 'aaa');
-- This calls window agg function multiple times
SELECT GROUP_CONCAT(b)
OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM tbl;
> aaa,bbb,ccc
> aaa,bbb,ccc
> aaa,bbb,ccc
-- This is NOT a window agg func call
-- The order appears correct, but is NOT defined per SQLite docs,
-- and can change in the future
SELECT group_concat(b) FROM (SELECT b FROM tbl ORDER BY b);
> aaa,bbb,ccc
-- This is NOT a window agg func call, and the result is wrong
SELECT group_concat(b) FROM tbl ORDER BY b;
> bbb,ccc,aaa
使用明确定义的顺序进行聚合的最可靠方法是将结果简单地限制为 1:
SELECT GROUP_CONCAT(b)
OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM tbl
LIMIT 1;
此外,上面可以用作子查询,例如如果即使表是空的也想获取一个值:
SELECT COALESCE(
(
SELECT GROUP_CONCAT(b)
OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING)
FROM tbl
LIMIT 1
),
'the table is empty'
);