Sql 如何根据列查找最后一次x连续计数

Sql 如何根据列查找最后一次x连续计数,sql,vertica,Sql,Vertica,列名:站点\u键。计费日期、原因、类型 要查找的内容: 1.Consecutive count for last 5 months: 2.Last 3 month Count 3.Last 6 month Count site_key | billing_date | Reason_type ----------+---------------------+--------------------- 3 | 2015-01-03 00:00:00 | AVERA

列名
站点\u键。计费日期、原因、类型

要查找的内容:

1.Consecutive count for last 5 months:
2.Last 3 month Count
3.Last 6 month Count
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;
我已根据条件原因类型找到过去5个月内站点密钥的连续计数

有4种不同的原因类型:零账单、相同单位、单位偏差、平均账单

我需要找到一个特定的站点密钥,比如ex:3,账单日期是2015年11月20日,原因类型为“零账单”

1.站点的零账单(不包括本月)有多少个连续的先前场景

2.最近3个月(包括本月)发生了多少次此类情况

3.过去6个月(包括本月)发生过多少次此类情况

样本数据:

1.Consecutive count for last 5 months:
2.Last 3 month Count
3.Last 6 month Count
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;
预期输出:

1.Consecutive count for last 5 months:
2.Last 3 month Count
3.Last 6 month Count
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;
我所尝试的: 对于连续计数:

1.Consecutive count for last 5 months:
2.Last 3 month Count
3.Last 6 month Count
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;
对于最后3个月和最后6个月:

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;

您使用的是什么版本的SQL?看起来像Oracle或SQL Server,但又不能确定。VERTICA是数据库这看起来很像。。。偶然的联系?几乎是一样的。我转载了详细的信息。有人能帮我吗?