简体   繁体   English

Oracle SQL:在组合两个 CASE WHEN 语句时使用 COUNT() >1

[英]Oracle SQL: Using COUNT() >1 When Combining two CASE WHEN Statements

I have a line of SQL which produces a count of purchases variable我有一行 SQL,它产生了购买变量的计数

count(distinct case when t.transaction_sub_type =1 then t.transaction_date end) as COUNTPUR, 

I need to modify this so I can produce a 0/1 flag variable, which flags if a customer is a repeat purchaser.我需要修改它,以便我可以生成一个 0/​​1 标志变量,它标记客户是否是重复购买者。 So, when a customer's purchases are greater than 1 then flag as 1 else flag as 0.因此,当客户的购买大于 1 时,则标记为 1,否则标记为 0。

case when COUNTPUR>1 then 1 else 0 end as FLAG_REPEATPURCHASER

I need to combine these two case statements into one.我需要将这两个 case 语句合二为一。 I have been experimenting with different versions of the syntax, but I can't seem to nail it down.我一直在尝试不同版本的语法,但我似乎无法确定它。 Below is one of the experiments which do not work.以下是无效的实验之一。

 max(case when (count(distinct case when t.transaction_sub_type =1 then t.transaction_date end))>1 then 1 else 0 end) as  FLAG_REPEATPURCHASER,

Thanks in advance for assitance提前感谢您的帮助

You can use a case expression with conditional aggregation:您可以使用带有条件聚合的case表达式:

(case when count(distinct case when t.transaction_sub_type = 1 then t.transaction_date end) > 1
      then 1 else 0
 end) as FLAG_REPEATPURCHASER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM