简体   繁体   English

如何在Oracle中获取多列的最大值

[英]How to get max of multiple columns in oracle

Here is a sample table: 这是一个示例表:

|           customer_token             |        created_date          | orders | views |
+--------------------------------------+------------------------------+--------+-------+
| 93a03e36-83a0-494b-bd68-495f54f406ca | 10-NOV-14 14.41.09.000000000 |    1   |   0   |
| 93a03e36-83a0-494b-bd68-495f54f406ca | 20-NOV-14 14.41.47.000000000 |    0   |   1   |
| 93a03e36-83a0-494b-bd68-495f54f406ca | 26-OCT-14 16.14.30.000000000 |    2   |   0   |
| 93a03e36-83a0-494b-bd68-495f54f406ca | 11-OCT-14 16.31.11.000000000 |    0   |   2   |

In this customer data table I store all of the dates when a given customer has placed an order, or viewed a product. 在此客户数据表中,我存储了给定客户下订单或查看产品时的所有日期。 Now, for a report, I want to write a query where for each customer (auth_token), I want to generate the last_order_date (row where orders > 0) and last_view_date (row where product_views > 0). 现在,对于一个报告,我想编写一个查询,其中要为每个客户(auth_token)生成last_order_date(行> 0的行)和last_view_date(product_views> 0的行)。

I am looking for an efficient query as I have millions of records. 我正在寻找一个有效的查询,因为我有数百万条记录。

select customer_token, 
       max(case when orders > 0 then created_date else NULL end),
       max(case when views > 0 then created_date else NULL end)
from Customer
group by customer_token;

Update : This query is quite efficient because Oracle is likely to scan the table only once. 更新 :此查询非常有效,因为Oracle可能只扫描一次表。 Also there is an interesting thing with grouping - when you use GROUP BY a select list can only contain columns which are in the GROUP BY or aggregate functions. 分组也有一件有趣的事情-使用GROUP BY时,选择列表只能包含GROUP BY或聚合函数中的列。 In this query MAX is calculated for the column created_date , but you don't need to put orders and views in a GROUP BY because they are in the expression inside MAX function. 在此查询中,将为created_date列计算MAX,但您无需将ordersviews放在GROUP BY中,因为它们位于MAX函数内部的表达式中。 It's not very common. 这不是很常见。

When you want to get the largest value from a row, you need to use the MAX() aggregate function. 如果要从一行中获取最大值,则需要使用MAX()聚合函数。 It is also best practice to group a column when you are using aggregate functions. 使用聚合函数时,最好将列分组。

In this case, you want to group by customer_token. 在这种情况下,您要按customer_token分组。 That way, you'll receive one row per group, and the aggregate function will give you the value for that group. 这样,您将在每个组中收到一行,并且聚合函数将为您提供该组的值。

However, you only want to see the dates where the cell value is greater than 0, so I recommend you put a case statement inside your MAX() function like this: 但是,您只想查看单元格值大于0的日期,因此我建议您在MAX()函数中放入一个case语句,如下所示:

SELECT customer_token, 
   MAX(CASE WHEN orders > 0 THEN created_date ELSE NULL END) AS latestOrderDate, 
   MAX(CASE WHEN views > 0 THEN created_date ELSE NULL END) AS latestViewDate
FROM customer
GROUP BY customer_token;

This will give you the max date only when orders is positive, and only when views is positive. 仅当订单为正时,并且仅当视图为正时,这将为您提供最大日期。 Without that case statement, the DBMS won't know which groups to give you, and you would likely get incorrect results. 没有该案例说明,DBMS将不知道该为您提供哪些组,并且您可能会得到错误的结果。

Here is an oracle reference for aggregate functions . 这是聚合函数的oracle参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM