ClickHouse 的 ORDER BY WITH FILL


ORDER BY Expr WITH FILL Modifier

可以在ORDER BY expr之后用可选的FROM expr,TO expr和STEP expr参数来设置WITH FILL修饰符。
所有expr列的缺失值将被顺序填充,而其他列将被填充为默认值。

使用以下语法填充多列,在ORDER BY部分的每个字段名称后添加带有可选参数的WITH FILL修饰符。

ORDER BY expr [WITH FILL] [FROM const_expr] [TO const_expr] [STEP const_numeric_expr], ... exprN [WITH FILL] [FROM expr] [TO expr] [STEP numeric_expr]

WITH FILL 仅适用于具有数字(所有类型的浮点,小数,整数)或日期/日期时间类型的字段。
当未定义 FROM const_expr 填充顺序时,则使用 ORDER BY 中的最小 expr 字段值。
如果未定义 TO const_expr 填充顺序,则使用 ORDER BY 中的最大expr字段值。
当定义了 STEP const_numeric_expr 时,对于数字类型,const_numeric_expr 将 as is 解释为 days 作为日期类型,将 seconds 解释为DateTime类型。
如果省略了 STEP const_numeric_expr,则填充顺序使用 1.0 表示数字类型,1 day表示日期类型,1 second 表示日期时间类型。

示例:

-- 表结构和数据准备
CREATE TABLE IF NOT EXISTS fillwith
(
    `event_timestamp` Datetime64,
    `event_date` Date,
    `event_type` String
)
ENGINE = Memory;
insert into fillwith (event_timestamp, event_date, event_type) values ('2021-01-07 19:14:33.000', '2021-01-07', 'PRODUCT_VIEW');
insert into fillwith (event_timestamp, event_date, event_type) values ('2021-02-07 19:14:33.000', '2021-02-07', 'PRODUCT_CLICK');
insert into fillwith (event_timestamp, event_date, event_type) values ('2020-11-07 19:14:33.000', '2020-11-07', 'PRODUCT_VIEW');
insert into fillwith (event_timestamp, event_date, event_type) values ('2020-12-07 19:14:33.000', '2020-12-07', 'PRODUCT_VIEW');
insert into fillwith (event_timestamp, event_date, event_type) values ('2020-09-07 19:14:33.000', '2020-09-07', 'PRODUCT_VIEW');

-- 常规group by 查询
SELECT 
    toDate(toStartOfInterval(event_date, toIntervalDay(1))) AS date,
    countIf(event_type = 'PRODUCT_VIEW') AS views,
    countIf(event_type = 'PRODUCT_CLICK') AS clicks
FROM fillwith
GROUP BY toDate(toStartOfInterval(event_date, toIntervalDay(1)))
ORDER BY date ASC;

查询结果

使用order by expr with fill modifier

SELECT
    toStartOfMonth(date) AS month,
    sum(views) AS views,
    sum(clicks) AS clicks
FROM 
(
    SELECT
        event_date AS date,
        countIf(event_type = 'PRODUCT_VIEW') AS views,
        countIf(event_type = 'PRODUCT_CLICK') AS clicks
    FROM fillwith
    GROUP BY date
    ORDER BY date ASC 
        WITH FILL 
            FROM toDate('2020-01-01') 
            TO toDate('2021-12-01') 
            STEP 1
)
GROUP BY month
ORDER BY month ASC
-- 或者 另一个 sql
WITH toDate(0) AS start_date, toRelativeMonthNum(toDate(0)) AS relative_month_of_start_date
SELECT
    addMonths(start_date, relative_month - relative_month_of_start_date) AS month,
    views,
    clicks
FROM 
(
    SELECT
        toRelativeMonthNum(event_date) AS relative_month,
        countIf(event_type = 'PRODUCT_VIEW') AS views,
        countIf(event_type = 'PRODUCT_CLICK') AS clicks
    FROM fillwith
    GROUP BY relative_month
    ORDER BY relative_month ASC 
        WITH FILL 
            FROM toRelativeMonthNum(toDate('2020-01-01'))
            TO toRelativeMonthNum(toDate('2021-12-01')) STEP 1
)
ORDER BY month ASC

查询结果

需要注意的是:查询必须至少有一条结果
1、空表的最终查询统计结果是空,不会进行排序填充
2、以上面数据为例,如果加入where条件,筛选2021年10月以后的数据统计(event_date >= toDate('2021-10-01')),那么无法填充,因为2021年10月之后的数据为空。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM