如何对含特定描述的发票下所有记录的成本求和
解决方案
1. SQL 实现
如果数据存储在数据库中,可通过子查询筛选目标发票号后求和:
SELECT SUM(cost) AS total FROM your_table WHERE invoice IN ( SELECT DISTINCT invoice FROM your_table WHERE description = '人工' );
2. Excel/Google Sheets 实现
假设数据位于A2:C6区域(A列invoice,B列description,C列cost),使用数组公式计算:
=SUM(IF(COUNTIFS(A:A,A2:A6,B:B,"人工")>0,C2:C6,0))
- Excel旧版本需按
Ctrl+Shift+Enter触发数组计算; - Excel 365/Google Sheets 直接回车即可。
3. Python Pandas 实现
用Pandas处理数据的代码示例:
import pandas as pd # 构造数据(实际可通过读取文件导入) df = pd.DataFrame({ 'invoice': [123, 123, 124, 125, 125], 'description': ['人工', '车间物料', '螺栓', '人工', '螺栓'], 'cost': [125.00, 25.00, 10.00, 100.00, 10.00] }) # 获取包含"人工"的发票编号集合 target_invoices = df[df['description'] == '人工']['invoice'].unique() # 计算目标发票的总成本 total_cost = df[df['invoice'].isin(target_invoices)]['cost'].sum() print(f"total: {total_cost:.2f}") # 输出:total: 260.00
内容的提问来源于stack exchange,提问作者user3221479
相关产品推荐
相关产品推荐

