简体   繁体   English

如何基于另一个列值在非聚合的SQL查询中返回最大值

[英]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.

相关问题 如何使用聚合列获取非聚合列的值? - How can I get the value of non-aggregated column with an aggregated column? 使用MySQL GROUP BY,如何控制在非聚合列中选择的默认值? - With a MySQL GROUP BY, how do I control the default value to choose in non-aggregated columns? 聚合查询并显示非聚合列的最高值 - Aggregate Query and display highest value of non-aggregated columns 如何在我的 PIVOT 值中为我的每个列添加一个额外的非聚合列? - How do I include an additional non-aggregated column for each of my in my PIVOT values? 如何在聚合和非聚合数据集上获得相同的 AVG() 值 - How to get the same value of AVG() on both aggregated & non-aggregated dataset 是否可以在SQL的聚合函数中包含未聚合的列,而不将其放入GROUP BY子句中? - Can I include a non-aggregated Column in an aggregate function in SQL without putting it into a GROUP BY clause? SQL 查询根据另一列中的最大值更新记录 - SQL query to update a record based on the max value in another column 如何根据另一列中的值找到最大值? SQL - How to find the max value in one column based on the value in another? SQL 显示基于另一列的值的汇总值 - Showing an aggregated value based on the value of another column BigQuery : GROUP BY 包含非聚合列 - BigQuery : GROUP BY include the non-aggregated column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM