我有一个简单的任务就是在'客户'之间进行联接。 (父表)和'命令' table(子表)子表的连接行具有最新(最大)订单日期值。如果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
我试图更换'和'与'其中'在子查询之间,但它没有帮助。
答案 0 :(得分:1)
您应该可以使用join
条款中的aggregation
和from
执行此操作:
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)
)
);