写一个sql计算药品的期初金额和期末金额,药品库存台账表是stock
时间: 2024-03-21 11:40:48 浏览: 27
假设药品库存台账表是"stock",其中包含以下字段:
- id:库存台账ID
- drug_id:药品ID
- quantity:数量
- cost:成本
- create_time:创建时间
则可以使用以下SQL语句计算药品的期初金额和期末金额:
```
-- 计算期初金额
select drug_id, sum(quantity * cost) as period_start_amount
from stock
where create_time < '2022-01-01'
group by drug_id;
-- 计算期末金额
select drug_id, sum(quantity * cost) as period_end_amount
from stock
where create_time >= '2022-01-01'
group by drug_id;
```
其中,第一个SQL语句计算的是时间点"2022-01-01"之前的期初金额,第二个SQL语句计算的是时间点"2022-01-01"之后的期末金额。需要注意的是,计算期初金额和期末金额时需要根据药品ID进行分组。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)