【问题标题】:MySQL how to calculate after stock takingMySQL盘点后如何计算
【发布时间】:2014-03-30 11:05:03
【问题描述】:

数据表如下方式;

Firm | PartID | StockCount |       Date | Type
-----------------------------------------------------------
   1 |     71 |          5 | 2014-02-01 | Incoming Invoice
   1 |     71 |         -1 | 2014-02-09 | Send Invoice
   1 |     71 |         10 | 2014-02-13 | Stocktaking  ( !!! Of the Depot. )
   1 |     71 |         -1 | 2014-02-21 | Send Invoice
   1 |     71 |          5 | 2014-02-28 | Incoming Invoice

此表实际上是一只股票,是对运动表的虚构描述。本表计入店内制作,购销发票包括在内。这样一来,入库、出库和入库的数量就会汇总到一张表中进行实际统计。从进行人口普查的那一刻起,股票价值应超过规定的金额。问题出在哪里。

我如何得到以下结果?

Firm | PartID | StockCount |       Date | Type
-------------------------------------------------------
   1 |     71 |         14 |       NULL | NULL

【问题讨论】:

  • 您需要按 First 或 PartID 或两者进行分组,认为输出中 StockCount = 14 的方式不清楚(不是总和)。

标签: mysql stock


【解决方案1】:

您似乎想要“盘点”之后的库存总和,我怀疑这在英语中更通常称为“做盘点”。

select Firm, PartId, sum(StockCount) as StockCount, NULL as Date, NULL as Type
from table t
where Date >= (select max(Date)
               from table t2
               where t2.Firm = t.Firm and
                     t2.partid = t.partid and
                     t2.type = 'Stocktaking'
              )
group by Firm, Partid;

如果可能没有盘点记录,则使用left join 方法:

select Firm, PartId, sum(StockCount) as StockCount, NULL as Date, NULL as Type
from table t left join
     (select Firm, PartId, max(Date) as maxDate
      from table t
      where t2.type = 'Stocktaking'
      group by Firm, PartId
     ) as tfp
     on t.Firm = tfp.Firm and t.PartId = tfp.PartId and t.Date >= tfp.MaxDate
group by t.Firm, t.PartId;

【讨论】:

  • 是的,我无法完全解释这个问题。但感谢您对该主题的兴趣。这张表实际上是一只股票,是对运动表的虚构描述。本表计入店内制作,购销发票包括在内。这样一来,入库、出库和入库的数量就会汇总到一张表中进行实际统计。从进行人口普查的那一刻起,股票价值应超过规定的金额。问题出在哪里。展示解决我问题的方法。谢谢。
【解决方案2】:

如果我没看错的话:

SELECT firm, partid, count(stockCount) as stock_total
FROM yourtable
WHERE firm = 1
AND partid = 71

如果要选择多个部分,则需要分组,例如:

SELECT firm, partid, count(stockCount) as stock_total
FROM yourtable
WHERE firm = 1
GROUP BY partid

【讨论】:

    【解决方案3】:

    试试这个

    SELECT Firm,
           PartID,
           Count(StockCount),
           Date AS NULL,
           TYPE AS NULL
    FROM TABLE
    GROUP BY Firm,
             PartID
    

    【讨论】:

      【解决方案4】:

      您对问题的解释不清楚,但我认为这是您想要的。

      SELECT Firm, PartID, SUM(StockCount) as StockCount, NULL as Date, NULL as Type
      FROM tbl T1
      WHERE Date >= (SELECT Date FROM tbl T2
                        WHERE T2.Type = Stocktaking
                         AND T1.Firm =T2.Firm
                         AND T1.PartId = T2.PartId
                    )
      GROUP BY Firm, PartID
      

      【讨论】:

        猜你喜欢
        • 2011-11-10
        • 1970-01-01
        • 1970-01-01
        • 2019-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-04
        • 2021-04-23
        相关资源
        最近更新 更多