【问题标题】:3 last record of each customer3 每个客户的最后记录
【发布时间】:2014-07-31 16:21:46
【问题描述】:

我有一张这样的客户和请求表:

客户表:

Key | Name
----+-----------
  1 | Roberto
  2 | Thiago
  3 | Mike 

请求表:

key | Date       | Customer
----+------------+------------  
  1 | 2012-02-07 | 1   
  2 | 2012-02-08 | 2
  3 | 2012-02-09 | 1
  4 | 2012-03-07 | 1
  5 | 2012-03-08 | 3
  6 | 2012-03-09 | 2
  7 | 2012-04-07 | 3
  8 | 2012-04-08 | 1
  9 | 2012-04-09 | 3

我想要一个返回每个客户的最后 3 个请求的查询。 Obs:我正在使用 MySQL 服务器

返回应该是这样的:

key | Date       | Customer
----+------------+-----------
  1 | 2012-02-07 | 1
  3 | 2012-02-09 | 1
  4 | 2012-03-07 | 1
  2 | 2012-02-08 | 2
  6 | 2012-03-09 | 2
  5 | 2012-03-08 | 3
  7 | 2012-04-07 | 3
  9 | 2012-04-09 | 3

我不能使用命令“TOP”,因为我使用的是 MySQL 服务器,而这个命令只能在 SQL Server 中使用。

【问题讨论】:

    标签: mysql sql greatest-n-per-group


    【解决方案1】:

    你可以试试

    select 
    r.`key`,
    r.Date,
    r.customer
    from Customers c
    left join 
    (
      select 
      r1.*
      from Requests r1
      where 
      (
        select count(*)
        from Requests r2
        where r1.customer = r2.customer
        AND r1.`key` <= r2.`key`
      ) <=3
      order by r1.Date desc
    )r
    on r.customer = c.`key`
    order by c.`key`
    

    另一种方式

    select
    r.`key`,
    r.Date,
    r.customer
    from Customers c
    join Requests r on r.`Customer` = c.`key`
    where
    (
      select count(*) from Customers c1
      join Requests r1 on r1.`Customer` = c1.`key`
      where
      c.`key` = c1.`key`
      and r1.`key`>= r.`key`
    ) <=3
    order by c.`key`,r.Date desc
    

    DEMO

    【讨论】:

      【解决方案2】:

      您可以查看此here

      SELECT DISTINCT `key`, date, customer
      FROM
      (
              SELECT  MAX(r1.`key`) `key`, MAX(r1.date) date, r1.customer customer
              FROM requests r1
              GROUP BY r1.customer
          UNION
              SELECT MAX(r2.`key`) `key`, MAX(r2.date) date, r1.customer customer
              FROM requests r1
              JOIN requests r2 ON  r2.customer = r1.customer
                                    AND r2.date < r1.date
              GROUP BY r1.customer
          UNION
              SELECT MAX(r3.`key`) `key`, MAX(r3.date) date, r1.customer customer
              FROM requests r1
              JOIN requests r2 ON  r2.customer = r1.customer
                                    AND r2.date < r1.date
              JOIN requests r3 ON r3.customer = r2.customer
                                    AND r3.date < r2.date
              GROUP BY r1.customer
      ) subquery
      ORDER BY customer, date, `key`
      

      【讨论】:

      • 我误解了这个问题。我用“last”来表示“最新”或“最近”的记录。无论如何,我更喜欢@Abhik-Chakraborty 的回答!
      【解决方案3】:

      我想这应该可行:

      SELECT     req.Key,
                 req.date,
                 req.customer
      FROM       [dbo].[Requests] req
      INNER JOIN [dbo].[Customers] cust ON cust.CustomerID = req.customerID
      GROUP BY   req.Key, req.date, req.customer
      ORDER BY   req.date
      

      希望对你有帮助!!!

      【讨论】:

      • 感谢 Satwik,但我使用的是 MySQL Server,无法使用 TOP,只能在 SQL Server 中使用。另一个建议?
      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2022-10-14
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多