如何在clickhouse中实现递归连接?

pbpqsu0x  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(0)|浏览(1102)

我有三张tablea、b和c。b有固定余额,而a有需要用固定余额更新的金额,并将这些合并记录推入c。ex.-b有100卢比的余额,a有三个记录50,-10,-40。我正在使用一个mview,它将a左连接b并更新c中的记录。
c所需输出:150140100
输出为c:150、90、60
因为它是一个联合体,它把所有三个记录的余额都取为100,这就是我得到150,90,60的原因。但我要150140100。
在clickhouse有什么办法吗?
a、b和c的模式是

DROP TABLE IF EXISTS A;
CREATE TABLE A
(
id UInt64,
`date` DateTime,
user_id UInt64,
status LowCardinality(String),
amount Float64,
sign Int8
) ENGINE = CollapsingMergeTree(sign) order by (user_id, id);

DROP TABLE IF EXISTS C;
create table C
(
 user_id UInt64,
 id UInt64,
 closing_balance Float64,
 amount Float64,
 status LowCardinality(String),
 `date` DateTime,
 sign Int8
) ENGINE = CollapsingMergeTree(sign) PARTITION BY toYYYYMM(date) 
order by (user_id, id);

DROP TABLE IF EXISTS B;
create table B
(
 user_id UInt64,
 id UInt64,
 closing_balance Float64,
 amount Float64,
 status LowCardinality(String),
 `date` DateTime,
 sign Int8
) ENGINE = CollapsingMergeTree(sign) order by (user_id);

DROP TABLE IF EXISTS B_mview;
CREATE MATERIALIZED VIEW B_mview to B
as SELECT * from C order by id asc;

插入查询是:

DROP TABLE IF EXISTS A_mview;
CREATE MATERIALIZED VIEW A_mview TO C AS SELECT A.company_id AS 
company_id,
    A.user_id AS user_id,
    A.id AS id,
    round((multiIf(A.status != 'Complete',
B.closing_balance,plus(A.amount,B.closing_balance))),2) AS 
closing_balance,
    A.amount AS amount,
    A.date AS date,
    toInt8(1) AS sign
FROM A LEFT JOIN B ON A.user_id=B.user_id

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题