简体   繁体   English

ORACLE SQL仅返回重复的值(不是原始值)

[英]ORACLE SQL Return only duplicated values (not the original)

I have a database with the following info 我有一个包含以下信息的数据库

Customer_id, plan_id, plan_start_dte,

Since some customer switch plans, there are customers with several duplicated customer_id s, but with different plan_start_dte . 由于某些客户转换计划,因此有些客户具有多个重复的customer_id ,但具有不同的plan_start_dte I'm trying to count how many times a day members switch to the premium plan from any other plan ( plan_id = 'premium' ). 我正在尝试计算每天成员有多少次从任何其他计划( plan_id = 'premium' )切换到高级计划。

That is, I'm trying to do roughly this: return all rows with duplicate customer_id , except for the original plan ( min(plan_start_dte) ), where plan_id = 'premium' , and group them by plan_start_dte . 也就是说,我正在尝试做大致的事情:返回所有具有重复的customer_id行,除了原始计划( min(plan_start_dte) )之外,其中plan_id = 'premium' ,并按plan_start_dte

I'm able to get all duplicate records with their count: 我能够获取所有重复记录及其计数:

with plan_counts as (
    select c.*, count(*) over (partition by CUSTOMER_ID) ct
    from   CUSTOMERS c
)
select *
from plan_counts
where ct > 1  

The other steps have me stuck. 其他步骤使我陷入困境。 First I tried to select everything except the original plan: 首先,我尝试选择除原始计划之外的所有内容:

SELECT CUSTOMERS c
where  START_DTE not in (
    select min(PLAN_START_DTE)
    from   CUSTOMERS i
    where  c.CUSTOMER_ID = i.CUSTOMER_ID
) 

But this failed. 但这失败了。 If I can solve this I believe all I have to add is an additional condition where c.PLAN_ID = 'premium' and then group by date and do a count. 如果我能解决这个问题,我相信我所c.PLAN_ID = 'premium'就是添加一个附加条件,其中c.PLAN_ID = 'premium' ,然后按日期分组并进行计数。 Anyone have any ideas? 有人有想法么?

I think you want lag() : 我想你想要lag()

select c.*
from (select c.*,
             lag(plan_id) over (partition by customer_id order by plan_start_date) as prev_plan_id
      from customers c
     ) c
where prev_plan_id <> 'premium' and plan_id = 'premium';

I'm not sure what output you want. 我不确定您想要什么输出。 For the number of times this occurs per day: 对于每天发生的次数:

select plan_start_date, count(*)
from (select c.*, lag(plan_id) over (partition by customer_id order by plan_start_date) as prev_plan_id
      from customers c
     ) c
where prev_plan_id <> 'premium' and plan_id = 'premium'
group by plan_start_date
order by plan_start_date;

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

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