Sql 在一个月内跟踪客户产品的变化
我需要计算客户在同一个月内为下列产品(语音、数据、短信)进行了多少次传输 下面是一些示例数据Sql 在一个月内跟踪客户产品的变化,sql,plsql,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
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
希望这是有意义的
关于您可以使用
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');