简体   繁体   中英

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

I have a line of SQL which produces a count of purchases variable

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. So, when a customer's purchases are greater than 1 then flag as 1 else flag as 0.

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

I need to combine these two case statements into one. 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 when count(distinct case when t.transaction_sub_type = 1 then t.transaction_date end) > 1
      then 1 else 0
 end) as FLAG_REPEATPURCHASER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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