简体   繁体   English

SQL:最大汇总和按查询分组

[英]SQL: Max aggregate & group by query

I am having troubles with my SQL query. 我的SQL查询有麻烦。 I have the following Orders table 我有以下Orders

 id
 order_number_for_user
 user_id
 date

I want to select the most recent order for a user on a specific date (2015-01-19). 我想选择一个特定日期(2015-01-19)用户的最新订单。 Here is some sample data 这是一些示例数据

 id| order_number_for_user | user_id |    created      
-------+---------------+---------+---------------
 1 |                     1 |      10 | 2015-01-19
 2 |                     2 |      10 | 2015-01-19
 3 |                     1 |      20 | 2015-01-19
 4 |                     1 |      30 | 2015-01-19
 5 |                     1 |      40 | 2015-01-19
 6 |                     2 |      40 | 2015-02-30

The correct output is the following row id's 2,3,4,5 正确的输出是以下行ID的2,3,4,5

Here is the broken query I built 这是我建立的坏查询

SELECT id FROM orders WHERE order_number_for_user = 
(SELECT max(order_number_for_user) FROM orders as orders2 
WHERE orders.user_id=orders2.user_id 
AND date(created) = date('2015-01-19'))

The rows are selected by this query are 1,2,3,4,5 . 该查询选择的行是1,2,3,4,5 Row 1 should not be selected since it is not the max order number for user 10 for the specified date. 不应该选择行1,因为它不是用户10在指定日期的最大订单号。

Can someone please tell me how my query is incorrect? 有人可以告诉我我的查询不正确吗?

You probably want this instead: 您可能想要这样:

SELECT * 
FROM orders
WHERE id IN (
   SELECT MAX(id)
   FROM orders
   WHERE DATE(created) = '2015-01-19'
   GROUP BY user_id
) AS max_order_ids

The inner/subquery gets the maximum ID for every user's orders on that particular day. 内部/子查询在该特定日期获取每个用户订单的最大ID。 Then the outer query fetches the rest of the order information for those IDs. 然后,外部查询将获取这些ID的其余订单信息。

Your query wasn't doing any grouping, so that MAX() aggregate has wonky results. 您的查询未进行任何分组,因此MAX()聚合产生了奇怪的结果。

Use an INNER JOIN : 使用一个INNER JOIN

SELECT O1.id
FROM Orders AS O1
INNER JOIN (SELECT user_id, MAX(order_number_for_user) order_number_for_user 
            FROM Orders
            WHERE date(created) = date('2015-01-19')
            GROUP BY user_id) AS O2
    ON O1.user_id = O2.user_id
    AND O1.order_number_for_user = O2.order_number_for_user
WHERE date(O1.created) = date('2015-01-19')

You need a sub query to find all user_id and its max order, then your query will work as follow: 您需要一个子查询来查找所有user_id及其最大顺序,然后您的查询将按以下方式工作:

SELECT id FROM orders, (SELECT user_id, max(order_number_for_user) maxOrderNum FROM orders GRPOUP BY user_id) as orders2 where orders.user_id=orders2.user_id and orders.order_number_for_user = orders2.maxOrderNum AND orders.date(created) = date('2015-01-19')) SELECT id FROM订单,(SELECT user_id,max(order_number_for_user)maxOrderNum FROM订单GRPOUP BY user_id)作为orders2,其中orders.user_id = orders2.user_id和orders.order_number_for_user = orders2.maxOrderNum和orders.date(created)= date('2015 -01-19' ))

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

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