获得状态的最高价值

时间:2013-07-19 07:45:01

标签: sql teradata

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

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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