join行与max行?

kq4fsx7k  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(401)

我有一个简单的任务,在“customer”(父表)和“order”表(子表)之间进行联接,子表的联接行具有最新(max)order date值。如果impala像任何sql引擎一样,您可以编写:

select * from customer c
join `order` o on o.customer_id=c.id
and o.id=(
    select o2.id 
    from `order` o2 
    where o2.customer_id=c.id 
    order by o2.order_date 
    desc limit 1
);

显然, Impala 是不同的,因为我只是得到以下错误:

Error while compiling statement: FAILED: ParseException line 4:1 cannot recognize input near 'select' 'o2' '.' in expression specification

我试图在子查询之间用where替换and,但是没有用。

zzlelutf

zzlelutf1#

你应该可以用一个 join 以及 aggregationfrom 条款:

select c.*, o.*
from customer c join
     `order` o
     on o.customer_id = c.id join
     (select customer_id, max(o2.order_date) as maxod
      from `order` o2
      group by customer_id
     ) oo
     on oo.customer_id = o.customer_id and oo.maxod = o.order_date;

这假设最大订单日期只有一个订单。如果这是不合理的,那么也许你可以使用 max(id) 而不是 max(order_date) . 如果ID是按顺序分配的,那么这将满足您的需要。
你也许可以用它做你想做的事 exists :

select c.*, o.*
from customer c join
     `order` o
     on o.customer_id = c.id
where not exists (select 1
                  from `order` o2
                  where o2.customer_id = o.customer_id and
                        (o2.order_date > o.order_date or
                         (o2.order_date = o.order_date and o2.id > o.id)
                        )
                 );

相关问题