在一个月内跟踪客户产品变更

时间:2014-09-16 02:25:52

标签: sql plsql

我需要计算客户在同一个月内针对以下列出的产品(语音,数据,短信)进行了多少次转账

以下是一些示例数据

Month        Customer No Product

01/Dec/2012  123         Voice
03/Dec/2012  123         Data
05/Dec/2012  345         Voice
09/Dec/2012  333         Voice
02/Dec/2012  333         data

这是预期的输出

Month   From_Product To_Product Customer_Total

Dec2012 Voice        Data       1
Dec2012 Data         Voice      1

希望这是有道理的。

问候

1 个答案:

答案 0 :(得分:3)

您可以使用lead()和聚合:

来处理此问题
select to_char(month, 'YYYY-MM') as mon, product as from_product, to_product,
       count(*) as customer_total
from (select sd.*,
             lead(product) over (partition by customer, to_char(month, 'YYYY-MM') order by month) as to_product
      from sampledata sd
     ) sd
where to_product is not null
group by to_char(month, 'YYYY-MM');