如何根据列

时间:2015-11-23 06:19:19

标签: sql vertica

列名称Site_key. Billing_date ,Reason_type

要找的东西:

1.Consecutive count for last 5 months:
2.Last 3 month Count
3.Last 6 month Count

我根据条件Reason_TYpe找到了site_key的最近5个月的连续计数。

There are 4 distinct Reason_type: Zero_bill,Same_Units,Units_Deviate,Average_Bill

我需要查找特定网站密钥,例如:3,billing_date表示20-11-2015,原因类型='Zero_Bill'

1.以前连续几个场景中有多个网站有Zero_Bill(不包括当月)

2.过去3个月(包括当前月份)有多少这样的情景

3.过去6个月(包括当前月份)中有多少这样的情景

示例数据:

site_key |    billing_date     | Reason_type 
----------+---------------------+---------------------
        3 | 2015-01-03 00:00:00 | AVERAGE_BILL
        3 | 2015-02-03 00:00:00 | ZERO_Bill  
        3 | 2015-03-01 00:00:00 | ZERO_Bill 
        3 | 2015-04-03 00:00:00 | AVERAGE_BILL
        3 | 2015-05-03 00:00:00 | AVERAGE_BILL
        3 | 2015-06-03 00:00:00 | ZERO_BILL
        3 | 2015-07-03 00:00:00 | ZERO_BILL
        3 | 2015-08-03 00:00:00 | AVERAGE_BILL
        3 | 2015-10-06 00:00:00 | ZERO_BILL

预期输出:

site_key |    billing_date     | Reason_type          |LAST_Consecutive | Last_3_Months | Last_6_months
----------+---------------------+----------------------- +----------------+--------------------+--------------------
        3 | 2015-01-03 00:00:00 | AVERAGE_BILL|0|1|1
        3 | 2015-02-03 00:00:00 | ZERO_Bill         |0|1|1
        3 | 2015-03-01 00:00:00 | ZERO_Bill             |1|2|2  
        3 | 2015-04-03 00:00:00 | AVERAGE_BILL|0|2|2
        3 | 2015-05-03 00:00:00 | AVERAGE_BILL|1|2|2
        3 | 2015-06-03 00:00:00 | ZERO_BILL     |0|2|3
        3 | 2015-07-03 00:00:00 | ZERO_BILL     |1|2|4
        3 | 2015-08-03 00:00:00 | AVERAGE_BILL|0|1|3
        3 | 2015-10-06 00:00:00 | ZERO_BILL|0|3|3

我尝试了什么: 连续统计:

Select site_key,
       billing_date,
       Reason_type, 
       rank() over (partition by site_key,billing_date,reason_type order by         billing_date) as consecutive_count
      from indus_Dev_analytics.AVG_SCN  
where site_key=3 
order  by 1,2;

对于Last_3_Months和Last_6_Months:

Select site_key,Billing_Date,Case when cnt>3 then 3 else cnt end as Count_OF_3, Case when cnt>6 then 6 else cnt end as Count_OF_6
from (
Select site_key,billing_date,Reason_TYPE,count(*) over (partition by site_key order by billing_date) as cnt from schema.AVG_SCN where reason_type='Zero_Bill' group by billing_date,site_key,Reason_TYPE
union all
Select site_key,billing_date,Reason_TYPE,count(*) over (partition by site_key order by billing_date) as cnt from Schema.AVG_SCN where reason_type='Average_Bill'  group by billing_date,site_key,Reason_TYPE
union all
Select site_key,billing_date,Reason_TYPE,count(*) over (partition by site_key order by billing_date) as cnt from schema.AVG_SCN where reason_type='Units_Deviate'  group by billing_date,site_key,Reason_TYPE
union all
Select site_key,billing_date,Reason_TYPE,count(*) over (partition by site_key order by billing_date) as cnt from schema.AVG_SCN where reason_type='Same_Units'  group by billing_date,site_key,Reason_TYPE) as a;

0 个答案:

没有答案