0

我有一组不同买家对不同卖家的购买数据,如下所示:

buyerid || sellerid || orderid || timestamp 

John123 || SellerABC || 123-abc-x1z || 26/07/2019
John123 || SellerABC || 123-abc-i9h || 28/07/2019
John123 || SellerABC || 123-abc-y16 || 28/07/2019
John123 || SellerDEF || 123-def-u13 || 30/07/2019
Bill456 || SellerABC || 456-abc-o34 || 02/08/2019
Bill456 || SellerABC || 456-abc-l3q || 09/08/2019
Bill456 || SellerABC || 456-abc-j5d || 10/08/2019
Bill456 || SellerDEF || 456-def-i61 || 11/08/2019

我希望能够在 SQL 中创建一个视图,以检索买家第一次从第二个卖家下订单的时间戳。如果没有来自第二个卖家的第一个订单,那么应该有一个空条目。结果视图应如下所示:

buyerid || first_order_second_seller_timestamp 

John123 || 30/07/2019
Bill456 || 11/08/2019

我想会有一些疯狂的分区和子查询来实现这一点,但任何帮助将不胜感激!目前我只能使用标准 SQL 函数检索第一个和最后一个订单:

SELECT
  "buyerid"
, "min"("timestamp") "first_order_timestamp"
, "max"("timestamp") "last_order_timestamp"
FROM
  default.order_table
GROUP BY "buyerid"
4

2 回答 2

0

嗯。. . 这有点棘手。这是一种使用方法lag()

select buyerid, min(timestamp)
from (select t.*,
             lag(sellerid) over (partition by buyerid order by timestamp) as prev_sellerid
      from order_table t
     ) t
where prev_sellerid <> sellerid   -- also filters out `NULL` values
group by buyerid;

要获取NULL值,请将过滤移至条件聚合:

select buyerid, min(case when prev_sellerid <> sellerid then timestamp end)
from (select t.*,
             lag(sellerid) over (partition by buyerid order by timestamp) as prev_sellerid
      from order_table t
     ) t
group by buyerid;

编辑:

您还可以使用两个级别的聚合:

select buyerid, min(case when timestamp = 2 then min_timestamp end)
from (select buyerid, sellerid, min(timestamp) as min_timestamp,
             row_number() over (partition by buyerid order by min(timestamp)) as seqnum
      from order_table t
      group by buyerid, sellerid
     ) bs
group by buyerid;

这也推广到第n 个卖家 ID。

于 2019-10-14T15:59:44.140 回答
0

您需要通过每个买家的卖家 ID 的排名获得第二个最大值

   Select "buyerid", sellerid, timestamp 
    from  ( 
    SELECT
    "buyerid", sellerid, timestamp
    Rank() over (partition by 
    buyerid, sellerid order by sellerid ) rn
    From table 

   FROM
   default.order_table) where rn=2
于 2019-10-14T16:07:58.367 回答