简体   繁体   中英

SUM in MySQL with ORDER BY and LIMIT

Suppose I have the following MySQL table:

在此处输入图片说明

I would like to know the total volume for Fuji Apples for the 3 days before the latest sale date (like a moving total). So for this example, I am after the total volume for my selection for the 3 days prior to the 04/01/14, which is 9.

I have made a number of attempts without the intended results:

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

I thought ORDER BY date DESC LIMIT 1,3 would work by restricting dates to 3 starting from the second last entry but it doesn't work.

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

Consider the following...

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM