Max stage using timestamp SQL Redshift

匿名 (未验证) 提交于 2019-12-03 01:42:02

问题:

I'd like to find the record associated with the max exited_on per application_id (sample table below).

I started out with the following SQL but get an error message telling me that my subquery has too many columns.

SELECT * FROM application_stages where application_stages.application_id = '91649746' and  (application_stages.application_id, max(exited_on) in (select application_stages.application_id, max(exited_on) from application_stages group by application_stages.application_id)) 

Table 1

+----------------+-------+--------------------+----------------+------------------+------------------+ | requisition_id | order |     stage_name     | application_id |    entered_on    |    exited_on     | +----------------+-------+--------------------+----------------+------------------+------------------+ | a              |     0 | Application Review |       91649746 | 6/8/2018 18:27   | 8/28/2018 22:04  | | a              |     1 | Recruiter Screen   |       91649746 | 6/8/2018 18:27   | 6/21/2018 0:17   | | a              |     2 | Phone Interview    |       91649746 | 6/21/2018 0:17   | 7/18/2018 12:17  | | a              |     3 | Assessment         |       91649746 |                  |                  | | a              |     4 | Interview          |       91649746 |                  |                  | | a              |     5 | Interview 2        |       91649746 |                  |                  | | a              |     6 | Interview 3        |       91649746 |                  |                  | | a              |     7 | Offer              |       91649746 |                  |                  | | a              |     0 | Application Review |       91991364 | 6/13/2018 14:21  | 6/19/2018 23:56  | | a              |     1 | Recruiter Screen   |       91991364 | 6/19/2018 23:56  | 9/4/2018 14:01   | | a              |     2 | Phone Interview    |       91991364 |                  |                  | | a              |     3 | Assessment         |       91991364 |                  |                  | | a              |     4 | Interview          |       91991364 |                  |                  | | a              |     5 | Interview 2        |       91991364 |                  |                  | | a              |     6 | Interview 3        |       91991364 |                  |                  | | a              |     7 | Offer              |       91991364 |                  |                  | | b              |     0 | Application Review |       96444221 | 8/8/2018 16:59   | 8/14/2018 5:42   | | b              |     1 | Recruiter Screen   |       96444221 | 8/14/2018 5:42   | 10/16/2018 20:02 | | b              |     2 | Phone Interview    |       96444221 |                  |                  | | b              |     3 | Interview          |       96444221 | 10/16/2018 20:02 | 10/24/2018 4:27  | | b              |     4 | Interview 2        |       96444221 | 10/24/2018 4:27  | 11/5/2018 22:38  | | b              |     5 | Offer              |       96444221 |                  |                  | +----------------+-------+--------------------+----------------+------------------+------------------+ 

回答1:

As pointed out, IN works just with a single column subquery. To accomplish what you want, you can use a join:

SELECT t1.* FROM application_stages t1 JOIN (     select application_id, max(exited_on) as exited_on     from application_stages      group by application_id ) t2  USING (application_id,exited_on) 


回答2:

You can't use multiple columns in "IN" operator. More information: https://www.w3schools.com/sql/sql_in.asp

If you're looking for recent exited record for an application you could use this instead.

SELECT * FROM (     SELECT a.*      , RANK() OVER (PARTITION BY a.application_id ORDER BY exited_on DESC) AS max_exited     FROM application_stages a ) WHERE max_exited = 1; 

This uses window function. More information on that: https://docs.aws.amazon.com/redshift/latest/dg/r_Examples_of_rank_WF.html

If you want to handle null values when using window function you could refer this: https://stackoverflow.com/a/22308104/3294216



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!