3

我找到了一些示例,展示了如何从分组集中选择单个最旧/最新行,但是在从数据集中获取最旧的两行时遇到了麻烦。

这是我的示例表:

CREATE TABLE IF NOT EXISTS `orderTable` (
  `customer_id` varchar(10) NOT NULL,
  `order_id` varchar(4) NOT NULL,
  `date_added` date NOT NULL,
  PRIMARY KEY (`customer_id`,`order_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `orderTable` (`customer_id`, `order_id`, `date_added`) VALUES
('1234', '5A', '1997-01-22'),
('1234', '88B', '1992-05-09'),
('0487', 'F9', '2002-01-23'),
('5799', 'A12F', '2007-01-23'),
('1234', '3A', '2009-01-22'),
('3333', '7FHS', '2009-01-22'),
('0487', 'Z33', '2004-06-23'),
('3333', 'FF44', '2013-09-11'),
('3333', '44f5', '2013-09-02');

此查询返回多于两行:

SELECT customer_id, order_id, date_added
FROM orderTable T1
WHERE (
   select count(*) FROM orderTable T2
   where T2.order_id = T1.order_id AND T2.date_added <= T1.date_added
) <= 2;

由于我不是在寻找单行,因此这不是标准greatest-n-per-group类型查询。

我可以为每个 customer_id 获得前两个订单,我错过了什么?

4

4 回答 4

3

最好的(即性能最高的)方法是在查询中使用用户定义的变量。

SELECT tmp.customer_id, tmp.date_added 
FROM ( 
  SELECT 
    customer_id, date_added, 
    IF (@prev <> customer_id, @rownum := 1, @rownum := @rownum+1 ) rank,
    @prev := customer_id
  FROM orderTable t 
  JOIN (SELECT @rownum := NULL, @prev := 0) r 
  ORDER BY t.customer_id
) tmp 
WHERE tmp.rank <= 2 
ORDER BY customer_id, date_added

结果

| CUSTOMER_ID |                       DATE_ADDED |
|-------------|----------------------------------|
|        0487 |   January, 23 2002 00:00:00+0000 |
|        0487 |      June, 23 2004 00:00:00+0000 |
|        1234 |       May, 09 1992 00:00:00+0000 |
|        1234 |   January, 22 1997 00:00:00+0000 |
|        3333 |   January, 22 2009 00:00:00+0000 |
|        3333 | September, 02 2013 00:00:00+0000 |
|        5799 |   January, 23 2007 00:00:00+0000 |

在这里拉小提琴。

请注意,连接仅用于初始化变量。

于 2013-09-13T21:46:21.687 回答
3

您的原始查询应该是(在子查询中使用 customer_id)

SELECT customer_id, order_id, date_added
FROM orderTable T1
WHERE (
   select count(*) FROM orderTable T2
   where T2.customer_id = T1.customer_id AND T2.date_added <= T1.date_added
) <= 2;

您还可以使用变量:

SELECT customer_id, order_id, date_added FROM (
SELECT customer_id, order_id, date_added,
@rownum := if(@prev_cust = customer_id, @rownum + 1,1) as rn,
@prev_cust := customer_id cust_var
FROM orderTable T1,
  (SELECT @rownum := 0) r,
  (SELECT @prev_cust := '') c
order by customer_id, date_added 
) o where o.rn < 3;

SQL 演示

于 2013-09-13T21:48:45.767 回答
0

这应该会产生您所追求的结果,但外部SELECT不会是最有效的,因为它在派生表上进行过滤。

SELECT ranked.* 
FROM (
    SELECT ot.* , 
        @rownum := IF( ot.customer_id = @previous , @rownum +1, 1 ) rank, 
        @previous := ot.customer_id
    FROM orderTable ot, 
        (SELECT @rownum :=1, @previous := NULL) init
    ORDER BY customer_id, date_added
) ranked
WHERE rank <=2
于 2013-09-13T22:37:38.527 回答
0

这是另一种(故意不完整)方法,尽管其他人可能对性能有所了解......

SELECT x.*
     , COUNT(*) rank 
  FROM ordertable x 
  JOIN ordertable y 
    ON y.customer_id = x.customer_id 
   AND y.date_added <= x.date_added 
 GROUP 
    BY x.customer_id 
     , x.date_added;
于 2013-09-13T21:51:34.887 回答