本文介绍了SQL - 选择分组行的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我有一个对行进行分组的查询.产品 ID 相同但日期和数量不同的多行被分组,以便我可以计算数量、平均价格等的总和.
I have a query where rows are grouped. Multiple rows where the product ID are the same but where date and quantity differs, are grouped so I can calculate the sum of quantities, average prices, etc.
我现在需要添加上次销售的日期和相应的数量.日期加了MAX(date),怎么加对应的数量?
I now need to add the date when the last sale occurred and the corresponding quantity. I added the date with MAX(date), but how do I add the corresponding quantity?
我使用 MS SQL Server Management Studio.
I use MS SQL Server Management Studio.
示例查询:
原始样本数据:
预期结果:
推荐答案
你可以使用窗口函数
:
输出:
DBFiddle 演示
这篇关于SQL - 选择分组行的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!