mysql Clickhouse中的CASE条件问题

qyswt5oh  于 2023-01-08  发布在  Mysql
关注(0)|答案(1)|浏览(186)

我在mySQL中编写了这段代码,它在那里运行良好。但在CASE条件下,clickhouse db中存在一些语法问题:

select
BTOP.id,
BTOP.customer_id,
BTOP.brand_id,
(case when (SELECT count(customer_id) from c_bill_transaction where customer_id = BTOP.customer_id AND added_on < '2023-01-01 00:00:00') <=0 then "new" else "exiting" end) as  "customer_type",
BT.id,
BT.status,
BTOP.added_on
from c_bill_transaction_online_precheckout BTOP
Left Join c_bill_transaction BT on BT.precheckout_id = BTOP.id
where BTOP.added_on > '2023-01-01 00:00:00'

Clickhouse抛出错误:

ClickHouse exception, code: 1002, host: 127.0.0.1, port: 8123; Code: 47. DB::Exception: Missing columns: 'BTOP.customer_id' while processing query: 'SELECT count(customer_id) FROM c_bill_transaction WHERE (customer_id = BTOP.customer_id) AND (added_on < '2023-01-01 00:00:00')', required columns: 'customer_id' 'BTOP.customer_id' 'added_on', maybe you meant: ['customer_id','customer_id','added_on']: While processing (SELECT count(customer_id) FROM c_bill_transaction WHERE (customer_id = BTOP.customer_id) AND (added_on < '2023-01-01 00:00:00')) AS _subquery8: While processing ((SELECT count(customer_id) FROM c_bill_transaction WHERE (customer_id = BTOP.customer_id) AND (added_on < '2023-01-01 00:00:00')) AS _subquery8) <= 0: While processing multiIf(((SELECT count(customer_id) FROM c_bill_transaction WHERE (customer_id = BTOP.customer_id) AND (added_on < '2023-01-01 00:00:00')) AS _subquery8) <= 0, new, exiting) AS customer_type. (UNKNOWN_IDENTIFIER) (version 22.7.1.2484 (official build))
42fyovps

42fyovps1#

您正在使用引用外部查询中列值的子查询。这称为相关子查询,clickhouse尚不支持。根据https://github.com/ClickHouse/ClickHouse/issues/6697,今年可能会支持相关子查询。
您可以改为联接c_bill_transaction并按BTOP.id分组。

select
    BTOP.id,
    BTOP.customer_id,
    BTOP.brand_id,
    (case when count(BTcust.customer_id) then "new" else "exiting" end) as  "customer_type",
    BT.id,
    BT.status,
    BTOP.added_on
from c_bill_transaction_online_precheckout BTOP
Left Join c_bill_transaction BT on BT.precheckout_id = BTOP.id
left join c_bill_transaction BTcust on BTcust.customer_id=BTOP.customer_id and BTcust.added_on < '2023-01-01 00:00:00'
where BTOP.added_on > '2023-01-01 00:00:00'
group by BTOP.id

相关问题