如何在oracle中选择两个max函数

时间:2014-11-13 02:23:03

标签: oracle select max

我需要在这里从两个表中选择max。我怎样才能做到这一点?这是我的oracle代码:

select t_order_demand.customer_name,
max(t_order_log.seq) as "seq",
max(t_order_log.extern_order_status) as "extern"
from t_order_demand,t_order_log
where t_order_demand.order_id=t_order_log.order_id
and t_order_demand.order_id like '%88%'
group by t_order_log.seq,t_order_log.extern_order_status,t_order_demand.customer_name;

结果是有两个客户名称,其中seq列为1,并且有两个extern 0和2.我只希望它们显示1个客户名称,1个最大seq和1个最大extern。但是怎么样?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您需要从GROUP BY中删除seq,extern_order 还将其更改为使用显式连接

    select t_order_demand.customer_name,
    max(t_order_log.seq) as "seq",
    max(t_order_log.extern_order_status) as "extern"
    from t_order_demand
    JOIN t_order_log
    ON t_order_demand.order_id=t_order_log.order_id
    and t_order_demand.order_id like '%88%'
    group by t_order_demand.customer_name

如果您只想要一个客户

SELECT * FROM  (
  select t_order_demand.customer_name,
  max(t_order_log.seq) as "seq",
  max(t_order_log.extern_order_status) as "extern"
  from t_order_demand
  JOIN t_order_log
  ON t_order_demand.order_id=t_order_log.order_id
  and t_order_demand.order_id like '%88%'
  group by t_order_demand.customer_name 
) WHERE ROW_NUM =1