sqldatabasedatediffclickhouse

ClickHouse Columns are from different tables while processing dateDiff


I'm trying to calc login user next day retention with ClickHouse.

The table structure of t_user_login is:

┌─name────┬─type──────────────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ user    │ String                    │              │                    │         │                  │                │
│ log_day │ DateTime('Asia/Shanghai') │              │                    │         │                  │                │
└─────────┴───────────────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

And the SQL is:

SELECT DISTINCT log_day,a.user as user_day0,b.user as user_day1
FROM (
  SELECT min(log_day) as log_day, user
  FROM t_user_login
  GROUP BY user
) a
LEFT JOIN t_user_login b
ON dateDiff('day', b.log_day, a.log_day) = 1 AND a.user = b.user;

But received an exception:

Received exception from server (version 20.11.4): Code: 403. DB::Exception: Received from localhost:9000. DB::Exception: Invalid columns in JOIN ON section. Columns b.log_day and log_day are from different tables.: While processing dateDiff('day', b.log_day, log_day) = 1.

This really confused me for a long time. Anyone can help me, thanks.


Solution

  • SELECT DISTINCT log_day,a.user as user_day0,b.user as user_day1
    FROM (
      SELECT min(log_day) as log_day, user
      FROM t_user_login
      GROUP BY user
    ) a
    LEFT JOIN t_user_login b
    ON toStartOfDay(b.log_day - interval 1 day) =toStartOfDay(a.log_day) AND a.user = b.user;