【问题标题】:SUM in MySQL with ORDER BY and LIMIT在 MySQL 中使用 ORDER BY 和 LIMIT 求和
【发布时间】:2014-06-22 04:22:18
【问题描述】:

假设我有以下 MySQL 表:

我想知道最近销售日期前 3 天的富士苹果的总销量(如移动总量)。所以对于这个例子,我在 2014 年 4 月 1 日之前 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 来工作,但它不起作用。

【问题讨论】:

  • 您要的是前 3 天的总销售额,还是仅前 3 天的总销售额,无论这些天是否售出任何富士苹果。
  • 另外,您的数据集对问题的“代表性不足”,不是吗?
  • 我是在前 3 天的总数之后,无论是否有富士苹果销售(在示例中,02/01 没有销售)。
  • 您在同一日期有两个相同类型的销售。所以,你没有(自然)主键。

标签: php mysql sql


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    考虑以下...

     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

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2014-09-10
      • 2017-05-12
      • 1970-01-01
      相关资源
      最近更新 更多