登录
首页 >  Golang >  Go问答

将使用 clickhouse 进行带有条件计数的 Postgres 查询转换

来源:stackoverflow

时间:2024-02-08 09:00:28 325浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《将使用 clickhouse 进行带有条件计数的 Postgres 查询转换》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我需要将此 postgres 查询转换为 clickhouse:

select count(1) 过滤器(其中 car_model = 'chevrolet')、count(distinct car_num) 过滤器(其中 car_model='chevrolet');

我尝试过使用 countIf,但它不接受该条件。所以我需要一个选择。


正确答案


不接受条件是什么意思?

它按预期工作:

create table cars
(
    `car_model` string,
    `car_num` integer
)
engine = mergetree
order by tuple();

insert into cars values ('chevrolet', 1),  ('chevrolet', 1), ('opel', 2), ('chevrolet', 3), ('dodge', 4), ('subaru', 16);

查询:

select
    countif(1, car_model = 'chevrolet'),
    countifdistinct(car_num, car_model = 'chevrolet')
from cars

结果:

Row 1:
──────
countIf(1, equals(car_model, 'chevrolet')):           3
uniqExactIf(car_num, equals(car_model, 'chevrolet')): 2

1 row in set. Elapsed: 0.024 sec.

终于介绍完啦!小伙伴们,这篇关于《将使用 clickhouse 进行带有条件计数的 Postgres 查询转换》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>