2

fifo_costとの差に基づいて、特定の注文に権利を割り当てようとしています。 と の最小の差に関連付けられfifo_in_dateている をその注文に割り当てる必要があります。order_datefifo_costorder_datefifo_date_in

次の mysql スニペットはレコードを返しません。fifo_date_inに最も近い1 つのレコードが返されることを期待しています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

クエリの問題は、having ステートメントです。

あなたがしたいことはMIN(datediff(order_date,fifo_date_in)) as ddiff、差の最小値を見つける選択です。次に、 your を使用group 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 に答える