Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在一个月内跟踪客户产品的变化_Sql_Plsql - Fatal编程技术网

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');