[英]How do I return max value in a non-aggregated sql query based on another column value
I've recently learned how to return max value using ARRAY_AGG
function with aggregated query ( by aggregated I mean when there is a group by
function at some point in the query). 我最近学习了如何使用带有聚合查询的ARRAY_AGG
函数返回最大值(通过聚合,我的意思是在查询中的某个时刻存在group by
功能group by
)。 I recently tried to apply the ARRAY_AGG
function with a non-aggregated query to also return max value and It didn't work as I was expecting. 最近,我尝试将ARRAY_AGG
函数与非聚合查询一起使用,以返回最大值,并且它没有按我期望的那样工作。 So my question is : how do I get the max value of a column based on another column input. 所以我的问题是:如何根据另一个列输入获取列的最大值。
To add more context to my question here is the situation. 要为我的问题添加更多背景信息是这里的情况。 Let say you have a user who is updating his status. 假设您有一个正在更新其状态的用户。 I want a query where I can have is most recent status but at the same time his second most recent one (or the one before his most recent update). 我想查询一个我可以拥有的最新状态,但同时又是他的第二个最近状态(或他最近的更新之前的状态)的查询。
so here is the table : 所以这是表格:
accountKey paymentType date Status
51135473 transaction 2016-10-16 Mini
51135473 UpdateStatus 2016-09-22 Mini
51135473 transaction 2016-12-08 Standard
51135473 transaction 2016-11-08 Standard
51135473 UpdateStatus 2016-11-08 Standard
Here is the result I want : 这是我想要的结果:
accountKey paymentType date Status CurrentStatus PreviousStatus
51135473 transaction 2016-10-16 Mini Standard Mini
51135473 UpdateStatus 2016-09-22 Mini Standard Mini
51135473 transaction 2016-12-08 Standard Standard Mini
51135473 transaction 2016-11-08 Standard Standard Mini
51135473 UpdateStatus 2016-11-08 Standard Standard Mini
Last status is simply the very last status in set. 最后状态只是集合中的最后一个状态。 The previous status is the status very the current one. 先前的状态是当前的状态。 The global idea is to have for each UpdateStatus paymentType at the same time the current status and the previous one. 全局想法是为每个UpdateStatus paymentType同时具有当前状态和上一个状态。
Like I said I've tried to use the following query : 就像我说过的,我尝试使用以下查询:
SELECT
accountKey,
paymentType,
date,
status,
ARRAY_AGG(status ORDER BY date DESC LIMIT 1)[OFFSET (0)] CurrentStatus
FROM
Table
GROUP BY
accountKey,
paymentType,
receivedOn
However this query makes little sense here since I don't need to group anything. 但是,此查询在这里毫无意义,因为我不需要对任何内容进行分组。 I'm grouping here because otherwise I can run the query. 我在这里分组,因为否则我可以运行查询。 I understand that I have to add Group by
function because I'm using ARRAY_AGG
. 我知道我必须添加Group by
功能Group by
因为我正在使用ARRAY_AGG
。 So my question again, how to I replace the ARRAY_AGG
function I'm using here in a non-grouped query. 所以我的问题再次出现,即如何替换在非分组查询中使用的ARRAY_AGG
函数。
Hopefully all this will make sense. 希望所有这些都有意义。
Thanks. 谢谢。
You can use nth_value()
: 您可以使用nth_value()
:
select t.*,
nth_value(status, 1) over (partition by accountkey order by date desc) as current_status,
nth_value(status, 2) over (partition by accountkey order by date desc) as current_status_but_one
from t;
If you just wanted the current status, I would recommend first_value()
. 如果您只是想要当前状态,我建议first_value()
。 But you are looking for multiple statuses, so you might as well use the same function for all of them. 但是您正在寻找多种状态,因此您最好对所有状态使用相同的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.