2

我正在尝试根据和之间的差异为给定订单分配权利:fifo_cost关联到和之间的最小差异应该分配给该订单。fifo_in_dateorder_datefifo_costorder_datefifo_date_in

以下 mysql 片段不会返回任何记录。我希望它能够返回fifo_date_in最接近 的记录order_date,但显然我遗漏了一些东西。

drop table if exists tmp;

create table tmp (
order_sequence int,
order_number int,
order_date date,
fifo_date_in date,
fifo_cost float);

INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-01-01',1.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-02-01',2.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-03-01',3.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-04-01',4.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-05-01',5.55);

SELECT
  order_sequence, order_number, order_date, fifo_date_in, fifo_cost, datediff(order_date,fifo_date_in) as ddiff
FROM tmp
GROUP BY order_sequence, order_number, order_date
HAVING datediff(order_date,fifo_date_in) = min(datediff(order_date,fifo_date_in))
4

3 回答 3

3

如果你想得到成本,我认为你必须找到最小值并加入基表:

SELECT t.order_sequence, t.order_number, t.order_date, t.fifo_date_in, t.fifo_cost
  FROM tmp t
  INNER JOIN ( SELECT order_sequence, order_number, order_date
                     ,MIN(datediff(order_date,fifo_date_in)) as ddiff
                 FROM tmp
                 GROUP BY order_sequence, order_number, order_date
              ) m
         ON (m.order_sequence = t.order_sequence
             AND m.order_number = t.order_number
             AND m.order_date = t.order_date
             AND datediff(t.order_date, t.fifo_date_in) = m.ddiff)

此外,如果最接近可以表示之前或之后,您可能必须考虑绝对值。

这是SQLFiddle

于 2013-06-25T01:01:56.867 回答
0

您的查询的问题是有语句。

您要做的是MIN(datediff(order_date,fifo_date_in)) as ddiff在选择中找到差异的最小值。然后,您使用 yourgroup by对结果进行分组。从之前的评论中划掉我的评论,您group by实际上是正确的:

drop table if exists tmp;

create table tmp (
order_sequence int,
order_number int,
order_date date,
fifo_date_in date,
fifo_cost float);

INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-01-01',1.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-02-01',2.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-03-01',3.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-04-01',4.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-05-01',5.55);


SELECT
  order_sequence, order_number, order_date, fifo_date_in, fifo_cost, MIN(datediff(order_date,fifo_date_in)) as ddiff
FROM tmp
GROUP BY order_sequence, order_number, order_date
于 2013-06-25T00:49:10.000 回答
0

您可以使用 ORDER BY 和 LIMIT,并省略 GROUP BY:

SELECT
  order_sequence, order_number, order_date, fifo_date_in, fifo_cost, datediff(order_date,fifo_date_in) as ddiff
FROM tmp 
ORDER BY ddiff
LIMIT 1
于 2013-06-25T00:59:01.140 回答