使用ORDER BY和LIMIT在MySQL中使用SUM

时间:2014-06-22 04:22:18

标签: php mysql sql

假设我有以下MySQL表:

enter image description here

我想知道最近一次销售日期前3天的富士苹果总量(如移动总量)。因此,对于这个例子,我是在2014年1月4日之前3天的选择总量之后,即9。

我做了很多尝试而没有预期的结果:

SELECT sum(volume) FROM (SELECT `volume` FROM `fruit_sale_db` WHERE `fruit` = 'apple' AND `type` = 'fuji') AS subquery ORDER BY `date` DESC LIMIT 1,3

我认为ORDER BY date DESC LIMIT 1,3可以通过从最后一个条目开始将日期限制为3来起作用,但它不起作用。

2 个答案:

答案 0 :(得分:0)

SELECT sum(volume) FROM `fruit_sale_db` where 'date' >= (latest_sale_date - 3) and 'date' <= latest_sale_date and `fruit` = 'apple' AND `type` = 'fuji' 

latest_sale_date类似

SELECT `date` FROM `fruit_sale_db` WHERE `fruit` = 'apple' AND `type` = 'fuji' ORDER BY `date` DESC LIMIT 1

答案 1 :(得分:0)

考虑以下内容......

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table 
 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,Fruit VARCHAR(12) NOT NULL
 ,Type VARCHAR(12) NOT NULL
 ,Date DATE NOT NULL
 ,Volume INT NOT NULL
 );

 INSERT INTO my_table (fruit,type,date,volume) VALUES
 ('Apple','Fuji' ,'2014-01-01',1),
 ('Apple','Other','2014-01-01',6),
 ('Apple','Fuji' ,'2014-01-01',2),
 ('Apple','Other','2014-01-02',1),
 ('Apple','Other','2014-01-02',4),
 ('Apple','Fuji' ,'2014-01-03',4),
 ('Apple','Other','2014-01-03',2),
 ('Apple','Fuji' ,'2014-01-04',8),
 ('Apple','Fuji' ,'2014-01-05',16),
 ('Pear' ,'Other','2014-01-06',1),
 ('Apple','Other','2014-01-06',4),
 ('Apple','Fuji' ,'2014-01-07',32),
 ('Apple','Other','2014-01-07',2),
 ('Apple','Fuji' ,'2014-01-08',64);

 SELECT * FROM my_table;
 +----+-------+-------+------------+--------+
 | id | Fruit | Type  | Date       | Volume |
 +----+-------+-------+------------+--------+
 |  1 | Apple | Fuji  | 2014-01-01 |      1 |
 |  2 | Apple | Other | 2014-01-01 |      6 |
 |  3 | Apple | Fuji  | 2014-01-01 |      2 |
 |  4 | Apple | Other | 2014-01-02 |      1 |
 |  5 | Apple | Other | 2014-01-02 |      4 |
 |  6 | Apple | Fuji  | 2014-01-03 |      4 |
 |  7 | Apple | Other | 2014-01-03 |      2 |
 |  8 | Apple | Fuji  | 2014-01-04 |      8 |
 |  9 | Apple | Fuji  | 2014-01-05 |     16 |
 | 10 | Pear  | Other | 2014-01-06 |      1 |
 | 11 | Apple | Other | 2014-01-06 |      4 |
 | 12 | Apple | Fuji  | 2014-01-07 |     32 |
 | 13 | Apple | Other | 2014-01-07 |      2 |
 | 14 | Apple | Fuji  | 2014-01-08 |     64 |
 +----+-------+-------+------------+--------+

 SELECT a.*
      , SUM(b.volume) rolling
      , GROUP_CONCAT(b.volume ORDER BY b.id DESC SEPARATOR '+' ) math
   FROM my_table a
   LEFT
   JOIN my_table b
     ON b.fruit = a.fruit
    AND b.type = a.type
    AND b.date BETWEEN a.date - INTERVAL 3 DAY AND a.date - INTERVAL 1 DAY
  GROUP
     BY a.id
  ORDER 
     BY fruit, type, id DESC;
 +----+-------+-------+------------+--------+---------+-------+
 | id | Fruit | Type  | Date       | Volume | rolling | math  |
 +----+-------+-------+------------+--------+---------+-------+
 | 14 | Apple | Fuji  | 2014-01-08 |     64 |      48 | 32+16 |
 | 12 | Apple | Fuji  | 2014-01-07 |     32 |      24 | 16+8  |
 |  9 | Apple | Fuji  | 2014-01-05 |     16 |      12 | 8+4   |
 |  8 | Apple | Fuji  | 2014-01-04 |      8 |       7 | 4+2+1 |
 |  6 | Apple | Fuji  | 2014-01-03 |      4 |       3 | 2+1   |
 |  3 | Apple | Fuji  | 2014-01-01 |      2 |    NULL | NULL  |
 |  1 | Apple | Fuji  | 2014-01-01 |      1 |    NULL | NULL  |
 | 13 | Apple | Other | 2014-01-07 |      2 |       4 | 4     |
 | 11 | Apple | Other | 2014-01-06 |      4 |       2 | 2     |
 |  7 | Apple | Other | 2014-01-03 |      2 |      11 | 4+1+6 |
 |  5 | Apple | Other | 2014-01-02 |      4 |       6 | 6     |
 |  4 | Apple | Other | 2014-01-02 |      1 |       6 | 6     |
 |  2 | Apple | Other | 2014-01-01 |      6 |    NULL | NULL  |
 | 10 | Pear  | Other | 2014-01-06 |      1 |    NULL | NULL  |
 +----+-------+-------+------------+--------+---------+-------+

http://sqlfiddle.com/#!2/5243e/1