按批次(n_lot)批量减法计算的SQL查询问题求助
解决方案
你的问题出在窗口函数未按批次(n_lot)分区,当前min(montat_soum) over()计算的是整个结果集的全局最小值,而非每个批次内的最小值。只需给窗口函数添加partition by n_lot子句,即可实现按批次取最小值并计算差值:
select T_AO.N_AO, n_lot, montat_soum , montat_soum - min(montat_soum) over(partition by n_lot) as resultat , designation_entr from t_soumission, t_entreprise, T_AO where T_AO.N_AO='02/STG/2023' and t_soumission.cod_entr=t_entreprise.cod_entr order by n_lot, resultat
关键修改说明
over(partition by n_lot):指定窗口函数按n_lot字段分组,每个分组内独立计算montat_soum的最小值,确保每个批次的差值基于自身批次的最小值计算。- 调整
order by为n_lot, resultat(可选):便于按批次查看结果,更清晰对比每个批次内的差值数据。
内容的提问来源于stack exchange,提问作者kheiro
相关产品推荐
相关产品推荐

