如何根据列中的值对表中的行进行分组/汇总

时间:2019-07-25 16:03:50

标签: sql aggregate amazon-redshift

我有一张买卖双方之间的聊天表,在许多情况下,同一买方和卖方之间(具有不同的聊天对象)之间的聊天具有不同的上次活动日期(在Unix时间中显示)。

The table as it currently looks

我的目标是能够查询该表,以便每个买家/卖家对都只返回一个聊天对象,并且该聊天对象对应于具有最近lastactivity的聊天对象-像这样:

The goal for the table

我尝试过:

SELECT max(lastactivity), chatid, buyerid, supplierid, 
FROM chat_table 
GROUP BY 2,3,4 

但这似乎不起作用...
有人能帮忙吗?

6 个答案:

答案 0 :(得分:2)

在Redshift中,我会使用窗口功能:

select ct.*
from (select ct.*,
             row_number() over (partition by least(buyerid, supplierid), greatest(buyerid, supplierid)
                                order by lastactivity
                               ) as seqnum
      from chat_table ct
     ) ct
where seqnum = 1;

答案 1 :(得分:1)

select a.lastactivity, a.buyerid, a.supplierid, a.chatid
from (SELECT max(lastactivity) lastactivity, buyerid, supplierid 
    FROM chat_table 
    GROUP BY 2,3) a
left join chat_table b on a.buyerid=b.buyerid and a.supplierid=b.supplierid and a.lastactivity=b.kastactivity;

答案 2 :(得分:1)

不存在:

SELECT c.* 
FROM chat_table c
WHERE NOT EXISTS (
  SELECT 1 FROM chat_table
  WHERE buyerid = c.buyerid AND supplierid = c.supplierid AND lastactivity > c.lastactivity
)

答案 3 :(得分:1)

您需要使用诸如row_number之类的窗口函数。 分区将为Buyer_id和Seller_id, 按lastactivity排序。

在外部查询中,过滤rownumber = 1的行。

https://docs.aws.amazon.com/redshift/latest/dg/c_Window_functions.html

答案 4 :(得分:1)

此问题被标记为aws-redshift。下面的查询工程。试试这个。

从chat_table中选择* 哪里(buyerid,sellerid,lastactivity) 在( 选择Buyerid,sellerid,max(lastactivity)作为lastactivity 来自chat_table 按买方编号,卖方编号分组);

答案 5 :(得分:1)

使用更简单的方法,使用自我加入,

SELECT a.buyerid, a.sellerid, a.chatid, a.lastactivity 
FROM chat_table a 
JOIN (SELECT MAX(lastactivity),buyerid, sellerid FROM chat_table GROUP BY buyerid, sellerid) b
ON a.buyerid = b.buyerid
AND a.sellerid = b.sellerid
AND a.lastactivity = b.lastactivity ;