我正在尝试从数据库中提取统计信息。表的结构是:
UpdatedId product_name revenue
980 Product1 1000
975 Product1 950
973 Product1 900
970 Product1 800
965 Product21 1200
所以收入=以前的收入+新的收入。
为了制作图表,目标是像这样获得 Product1 的输出
UpdateId Difference
980 50
975 50
973 100
970 0
我试过这个查询,但 MySQL 卡住了 :)
选择 a.product_name、a.revenue、b.revenue、b.revenue- a.revenue 作为与 updated_stats a、updated_stats b 的差异,其中 a.product_name=b.product_name 和 b.revenue= (select min(revenue) from updated_stats where product_name=a.product_name 和收入 > a.revenue 和 product_name='Product1')
你能告诉我,它应该如何查询?谢谢。