sql-server - 使用 sum 和内部联接的查询不起作用
问题描述
我不知道为什么这个 SQL 查询不能正常工作,它返回相同的记录而不使用sum
函数求和
select *, sum(VenteProduit.MontantHT) as 'TOTAL HT'
from RegFacture
inner join VenteProduit
on RegFacture.NumFacture = VenteProduit.NumFacture
group by VenteProduit.NumFacture,
RegFacture.NumFacture,
RegFacture.DateFacture,
RegFacture.ModePaiment,
RegFacture.DateEcheance,
RegFacture.TVA,
RegFacture.Devise,
RegFacture.MontantPayee,
RegFacture.RestMontant,
RegFacture.LieuLivraison,
RegFacture.incoterm,
RegFacture.Unite,
VenteProduit.RsClient,
VenteProduit.RefProduit,
VenteProduit.PrixVente,
VenteProduit.Quantitee,
VenteProduit.MontantHT
解决方案
使用窗口函数解决了问题
新的查询是:
select *,sum(VenteProduit.MontantHT) OVER(PARTITION BY VenteProduit.NumFacture) as 'TOTAL HT'
FROM [dbo].VenteProduit
inner join RegFacture on RegFacture.NumFacture=VenteProduit.NumFacture
推荐阅读
- dataframe - Pyspark Dataframe Column - 转换表示为列中字符串的十进制值
- shell - 如何使用 ssh 将谷歌云 shell 连接到 termux 应用程序?
- python - 从 swagger-codegen 生成的 python 服务器存根根本无法在本地工作
- c++ - 我们可以访问 BIF_NEWDIALOGSTYLE 浏览窗口使用的图标吗?
- python - 如果“if 语句”为 False,Tkinter 会打包空小部件
- mysql - SEQUELIZE - 尝试在表中插入数据时出错
- java - 在 ListView 中实现搜索功能时出错
- vimeo - 无需向 Vimeo API 发出请求即可获取 Vimeo 视频的缩略图
- .net - 无法从 ExceptionFilter 传递 TempData 值
- firebase - Firebase 存储规则资源变量返回 null