count函数与case

dluptydi  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(340)

计算第1-8阶段应计为sal的机会数,但我只想将第3-7阶段过滤为sql。
在子查询中尝试了case语句和计数,但没有成功。代码如下:

  1. SELECT camp, count(opp_id) as sal,
  2. count(opp_id (select opp_id from db.opp_data_q3 where stage not in ("01", "02")) as sql,
  3. from db.opp_data_q3
  4. where created_quarter = "Q3"
  5. group by camp;

预期结果:

  1. Camp A | SAL 10 | SQL 5
  2. Camp B | SAL 20 | SQL 3

实际结果是错误消息:
编译语句时出错:失败:parseexception行2:14无法识别函数规范中“select”“opp\u id”“from”附近的输入

5n0oy7gb

5n0oy7gb1#

我想你只需要条件聚合:

  1. select camp, count(*) as sal,
  2. sum(case when stage not in ('01', '02') then 1 else 0 end) as sql
  3. from db.opp_data_q3
  4. where created_quarter = 'Q3'
  5. group by camp;

相关问题