sql - 在 hivesql 中查找唯一计数 Postgresql 查询
问题描述
我想获得独特的客户数量。我有postgresql查询的参考。您能否将此查询转换为 HiveSql
SELECT
COUNT(user_id) Total_profiles,
COUNT(distinct user_id) FITLER (WHERE age BETWEEN 18 AND 12) as age_less_than_20
FROM
customer_profiles
WHERE
profile_date BETWEEN '2020-01-01' AND '2020-12-31'
解决方案
用例表达式:
SELECT
COUNT(user_id) Total_profiles,
COUNT(distinct case when age BETWEEN 18 AND 12 then user_id else null end) as age_less_than_20
FROM
customer_profiles
WHERE
profile_date BETWEEN '2020-01-01' AND '2020-12-31'
另一种计算不同值的方法是 size(collect_set()):
SELECT
COUNT(user_id) Total_profiles,
size(collect_set(case when age BETWEEN 18 AND 12 then user_id else null end)) as age_less_than_20
FROM
customer_profiles
WHERE
profile_date BETWEEN '2020-01-01' AND '2020-12-31'
推荐阅读
- git - 为什么gitea加了ssh还需要密码才能登录?
- node.js - 如何使用 sequelize 执行这个特定的 sql left join?
- react-native - React-Navigation v5 - 堆栈导航器之间的透明度
- javascript - 究竟是什么
用于 React 路由器? - reactjs - TypeScript:无法在类构造函数中为 this.x 赋值
- javascript - TypeError:无法读取未定义的属性“参数” - Dialogflow
- r - 嗨,我无法安装包 as.Date,因为 R 说 as.Date 在版本 3.6.2 中不可用
- java - 如何在部署 Ktor + Exposed 后端时正确使用 shadowJar?
- antlr4 - 我们能否期望在 ANTLR 4.8 或 5 中支持相互左递归规则?
- ruby - Selenium 自动关闭窗口