到目前为止我拥有的是: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 |发布于 2014-06-12 09:59:40
好了,我想我明白了。下面是产生我想要的结果的查询:
SELECT p.id
, a.nome
, p.preco
, date_format(p.`data`,'%m/%d/%Y') AS `data`
, COALESCE(p.quantidade-SUM(f.quantidade), p.quantidade) AS quantidade
, p.preco*COALESCE(p.quantidade-SUM(f.quantidade), p.quantidade) AS total
FROM acao_trans p
LEFT JOIN acao_trans f
ON p.id=f.parent
INNER JOIN acao a
ON p.id_acao=a.id
WHERE p.parent IS NULL
AND p.id_acao=1
AND p.id_empresa=9
GROUP BY p.id小提琴:http://sqlfiddle.com/#!2/bbfec/64。
我所做的:我加入了重要的表(acao_trans "p")和它本身("f"),我使用求和函数来汇总第二个论点的所有数量,给出所有卖出股票的总数。如果有关于"f“的记录,我想减去购买股票的总量("p")。如果没有通信,它将显示空字段,而我将显示购买数量。做完之后就很简单了。这里真正重要的是数量,这样我就能很容易地接触到其他的东西。
发布于 2014-06-11 11:25:40
从以下方面开始:
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”类型的交易更容易。
发布于 2014-06-11 13:39:29
为了便于理解,我翻译并调整了你们的库存表.
SELECT a.stock_id
, a.company_id
, a.transaction_date
, a.price
, COALESCE(a.quantity - SUM(b.quantity),a.quantity) quantity
, COALESCE(a.quantity - SUM(b.quantity),a.quantity) * a.price subtotal
FROM stock_company a
LEFT
JOIN
( SELECT x.stock_id
, x.company_id
, MAX(x.transaction_date) min_transaction_date
, y.quantity
FROM stock_company x
JOIN stock_company y
ON y.stock_id = x.stock_id
AND y.company_id = x.company_id
AND y.transaction_date <= x.transaction_date
AND y.transaction_type <> x.transaction_type
WHERE y.transaction_type = 'SELL'
GROUP
BY x.stock_id
, x.company_id
, y.quantity
) b
ON b.stock_id = a.stock_id
AND b.company_id = a.company_id
AND b.min_transaction_date = a.transaction_date
WHERE a.stock_id = 1
AND a.company_id = 9
AND a.transaction_type = 'BUY'
GROUP
BY stock_id
, company_id
, transaction_date;
+----------+------------+---------------------+-------+----------+----------+
| stock_id | company_id | transaction_date | price | quantity | subtotal |
+----------+------------+---------------------+-------+----------+----------+
| 1 | 9 | 2014-06-10 18:21:02 | 2.449 | 50 | 122.450 |
| 1 | 9 | 2015-05-12 04:29:29 | 2.240 | 50 | 112.000 |
+----------+------------+---------------------+-------+----------+----------+http://www.sqlfiddle.com/#!2/cfa4d/1
请注意,这还没有经过广泛的测试,因此可能有一个缺陷(或者几个缺陷!)在我的逻辑中,但在所提供的数据集上,它似乎工作得很好。
编辑:我做了一点小小的调整--仍然不确定是否足够。让我知道。
https://stackoverflow.com/questions/24160978
复制相似问题