SQL(对于 teradata)

SQL (for teradata)

如果我有如下数据集:

1/01/2014   a
2/01/2014   a
3/01/2014   a
4/01/2014   b
5/01/2014   b
6/01/2014   b
7/01/2014   b
8/01/2014   a
9/01/2014   a
10/01/2014  a
11/01/2014  a
12/01/2014  a
13/01/2014  a

如何获得如下所示的输出:

letter  min     max
a   1/01/2014   3/01/2014
b   4/01/2014   7/01/2014
a   8/01/2014   13/01/2014

Teradata 支持 window 函数。您需要计算一个组标识符。一种方法是行号不同:

select letter, min(date), max(date)
from (select t.*,
             (row_number() over (order by date) -
              row_number() over (partition by letter order by date)
             ) as grp
      from t
     ) t
group by letter, grp;