sql—子集和整集的case

h6my8fg2  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(294)

我试图计数事务,并在配置单元中用case-when语句标记它们。

select case    
  When PROTOCOLID = 61002 AND TRANS_TYPE = "succesfull"  THEN "X"   
When PROTOCOLID = 61002 AND (TRANS_TYPE ="succesfull" or TRANS_TYPE ="failed") THEN "Y"
   end as name, sum(count)
 from tab1
 group by name

但到目前为止,似乎只有更广泛的(succesfull+failed)when语句返回结果,而succesfull事务的语句没有返回结果。
编辑:我处理查询的次数越多,它似乎总是返回我放在第一位的when条件。假设我一共有10个事务,把成功事务的条件放在第一位,得到的结果如下:
7交易成功
3事务总数(成功+失败)
看来是这群人惹的祸

whhtz7ly

whhtz7ly1#

嗯。我可以通过以下方式获得这些信息:

select count(*) as cnt_total,
       sum(case when trans_type = 'successful' then 1 else 0 end) as cnt_success    
from tab1
where trans_type in ('succesful', 'failed') and
      protocolid = 61002;

不过,我怀疑你只是拼错了“successful”。

相关问题