admin管理员组文章数量:1355096
I have a table with users actions:
user_id | action | timestamp |
---|---|---|
1 | PAGEVIEW | 00:01 |
1 | PAGEVIEW | 00:02 |
1 | PAY | 00:05 |
I have a table with users actions:
user_id | action | timestamp |
---|---|---|
1 | PAGEVIEW | 00:01 |
1 | PAGEVIEW | 00:02 |
1 | PAY | 00:05 |
How do I create a materialized view to have timestamps of first user's appearance (when first action was made)?
In this case, the result should be 1 (user_id), 00:01 (first action)
It is like ReplacingMergeTree
backwards, I need to keep track of the first, not latest row, in my ordering.
2 Answers
Reset to default 1You may use a version column with calculated expression which value is higher on a lower timestamp
argument.
CREATE TABLE tab
(
user_id UInt32
, action LowCardinality(String)
, timestamp DateTime
) Engine = MergeTree()
ORDER BY user_id;
CREATE TABLE tab_rmt
(
user_id UInt32
, action LowCardinality(String)
, timestamp DateTime
, __ver UInt32
) Engine = ReplacingMergeTree(__ver)
ORDER BY user_id;
CREATE MATERIALIZED VIEW tab_mv TO tab_rmt AS
SELECT *, 4294967295 - toUnixTimestamp(timestamp) AS __ver
FROM tab;
INSERT INTO tab
SELECT *
FROM VALUES
(
'user_id UInt32
, action LowCardinality(String)
, timestamp DateTime'
, (1, 'PAGEVIEW', toDateTime('2025-01-01 00:01:00'))
, (1, 'PAGEVIEW', toDateTime('2025-01-01 00:02:00'))
, (1, 'PAY', toDateTime('2025-01-01 00:03:00'))
);
SELECT * FROM tab_rmt FINAL FORMAT Vertical;
The result is:
Row 1:
──────
user_id: 1
action: PAGEVIEW
timestamp: 2025-01-01 00:01:00
__ver: 2559277635 -- 2.56 billion
fiddle
A limit query might be what you want here:
SELECT *
FROM actions
WHERE user_id = 1
ORDER BY timestamp
LIMIT 1;
If instead you want a query for all users, then use ROW_NUMBER
:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY timestamp) rn
FROM actions
)
SELECT user_id, action, timestamp
FROM cte
WHERE rn = 1;
本文标签: Keep track of first row in ClickHouseStack Overflow
版权声明:本文标题:Keep track of first row in ClickHouse - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743965203a2569758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论