Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 每日SQL峰值_Mysql_Sql - Fatal编程技术网

Mysql 每日SQL峰值

Mysql 每日SQL峰值,mysql,sql,Mysql,Sql,我有下表: id | booking_time | art | weight ---+---------------------+-------+------ 1 | 2017-06-18 10:34:09 | wood | 1000 2 | 2017-06-18 11:31:11 | wood | 2000 3 | 2017-06-18 14:11:25 | stone | 1000 4 | 2017-06-18 16:4

我有下表:

   id |  booking_time       | art   | weight
   ---+---------------------+-------+------
    1 | 2017-06-18 10:34:09 | wood  |  1000
    2 | 2017-06-18 11:31:11 | wood  |  2000
    3 | 2017-06-18 14:11:25 | stone |  1000
    4 | 2017-06-18 16:47:37 | wood  |  -300
    5 | 2017-06-19 10:49:21 | wood  |   100
    6 | 2017-06-19 12:41:02 | wood  | -1000
    7 | 2017-06-19 12:49:54 | wood  |   200
我想得到的是每天库存的峰值:

   booking_day |  art  | peak
   ------------+-------+-------
    2017-06-18 | wood  | 3000
    2017-06-16 | stone | 1000
    2017-06-19 | wood  | 2800
因此,对于18.06,库存木材的最高数量为3000(1000+2000);在一天结束时,只剩下2700(1000+2000-300)人

对于19.06,我们从昨天的2700木材开始,以2800(2700+100)达到峰值;一天结束时剩下的金额为2000(2700+100-1000+200)


甚至可以使用SQL吗?可能我需要另一个表来存储每日金额或类似的内容

您需要计算一个累计金额,然后进行合计:

select date(booking_time), art, max(running_stock)
from (select t.*,
             (@s := if(@a = t.art, @s + weight,
                       if @a := t.art, weight, weight)
                      )
             ) as running_stock
      from t cross join
           (select @s := 0, @a := 0) params
      order by t.art, t.booking_time
     ) t
group by date(booking_time), art;

是的,我想是的。看到这样的想法了吗?如果你有问题,我们会帮助你。事实并非如此,您提供了一个规范,我们为您编写了它欢迎使用堆栈溢出。你可以考虑通过帮助部分来了解如何询问问题,以便得到有用的结果,以及适合的问题类型。这对于MySQL来说是非常简单的。你试过什么?您想在示例中显示每天可用的净流量还是结果?