1

Teradata 查询如下所示:

Select
a,
b,
MIN(Record_Start_Date) as MIN_Record_Start_Date,
MAX(Record_End_Date) as MAX_Record_End_Date
FROM tbl
GROUP BY 1,2

现在我想添加第三个属性“状态”。问题是-我只想要“最后一个”状态-“Record_End_Date”值最高的行的值。

有人可以帮忙吗?

4

1 回答 1

4

切换到 OLAP 函数而不是聚合:

SELECT
   a,
   b,
MIN(Record_Start_Date) OVER (PARTITION BY a, b) AS  MIN_Record_Start_Date,
Record_End_Date AS MAX_Record_End_Date,
Status
FROM tbl
QUALIFY 
   ROW_NUMBER() 
   OVER (PARTITION BY a,b 
         ORDER BY Record_End_date DESC) = 1
于 2013-07-19T08:22:23.523 回答