gpt4 book ai didi

mysql - 获取可供出售的股票

转载 作者:行者123 更新时间:2023-11-30 00:09:57 26 4
gpt4 key购买 nike

到目前为止我所拥有的:http://sqlfiddle.com/#!2/bbfec/6

我想获取给定公司可出售的给定股票的股票数量 - 按价格分组。例如,对于公司号 9 和股票号 1,我想要这样的数据:

 | id | name | price |           date        | quantity |  total  |
------------------------------------------------------------------
| 3 | ALTR | 2.240 | 2015-05-12 04:29:29 | 50 | 112.00 |
| 7 | ALTR | 2.449 | 2014-06-10 18:21:02 | 50 | 122.45 |

因为公司9在2015-05-12 04:29:29买入200只股票,在2014-06-10 15:50:17卖出100只股票,在2014-06-10 17:06:18又买入50只股票2014-06-10 18:21:02 50。

我不需要所有股票的总数,因为当公司收购它们时,它们的价格不同。价格和日期是采购价格和日期,但数量是某次采购剩余的数量。

提前致谢。

草莓,想要的结果:

 | id | price |     date     | quantity |
-----------------------------------------------
| 3 | 2.240 | 12-05-2015 | 50 |
| 7 | 2.449 | 10-06-2014 | 50 |

最佳答案

开头:

select id_acao, id_empresa, ifnull(bought,0) - ifnull(sold,0) as stock
from
(
select id_acao, id_empresa,
(select sum(quantidade) from acao_trans where tipo='C' and id_acao=a.id_acao and id_empresa=a.id_empresa) as bought,
(select sum(quantidade) from acao_trans where tipo='V' and id_acao=a.id_acao and id_empresa=a.id_empresa) as sold
from acao_trans a group by id_acao,id_empresa
) x
;
+---------+------------+-------+
| id_acao | id_empresa | stock |
+---------+------------+-------+
| 1 | 4 | 1500 |
| 1 | 9 | 100 |
| 8 | 9 | 3500 |
| 13 | 9 | 5000 |
+---------+------------+-------+

并将此查询加入到您的基本 acao 和 empresa 表中。

备注:出于统计等目的,使用负数量进行销售交易会比交易类型“C”和“V”更容易。

关于mysql - 获取可供出售的股票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24160978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com