SQL语句多表left join SUM出现的重复数据问题!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句多表left join SUM出现的重复数据问题!相关的知识,希望对你有一定的参考价值。

A表生产批号唯一

我做出来left join 结果领料数量会多出来 因为A对BCD表都是一对多的关系 C表与D表是多对多的关系

参考技术A select a.*,bb.领料数量,dd.完工数量
from a
left join (select 生产批号,sum(领料数量) as 领料数量 from b group by 生产批号) bb
on a.生产批号=b.生产批号
left join (select cc.生产批号,sum(d.完工数量) as 完工数量
from (select distinct 生产批号,rcid from c) cc,d
where c.rcid=d.rcid
group by c.生产批号
) dd on a.生产批号=dd.生产批号
;追问

消息 4104,级别 16,状态 1,第 1 行
无法绑定由多个部分组成的标识符 "b.生产批号"。
消息 4104,级别 16,状态 1,第 1 行
无法绑定由多个部分组成的标识符 "c.RCID"。
消息 4104,级别 16,状态 1,第 1 行
无法绑定由多个部分组成的标识符 "c.生产批号"。

追答

select a.*,bb.领料数量,dd.完工数量
from a
left join (select 生产批号,sum(领料数量) as 领料数量 from b group by 生产批号) bb
on a.生产批号=bb.生产批号
left join (select cc.生产批号,sum(d.完工数量) as 完工数量
from (select distinct 生产批号,rcid from c) cc,d
where c.rcid=d.rcid
group by c.生产批号
) dd on a.生产批号=dd.生产批号

本回答被提问者采纳

SQL-left join 左表数据多次出现,求解!

SELECT ph.PronoteHeaderId AS JGDN,ph.InvoiceCusId AS KHDH,pro.ProductName AS HPMC,ph.InvoiceXODetailQuantity AS DDSL,ph.DetailsSum AS SCSL,
(SELECT TOP 1 InvoiceXO.InvoiceYjrq FROM InvoiceXO WHERE InvoiceXO.InvoiceId = ph.InvoiceXOId) AS KHJQ,
CASE WHEN wh.Workhousename LIKE '%射出%' THEN ppd.PronoteProceduresDate END AS SCJQ,
CASE WHEN wh.Workhousename LIKE '%射出%' THEN isnull(pdd.HeJiProceduresSum,0) ELSE '' END AS SCHJSCSL,
CASE WHEN wh.Workhousename LIKE '%射出%' THEN isnull(pdd.HeJiCheckOutSum,0) ELSE '' END AS SCHJHGSL,
CASE WHEN wh.Workhousename LIKE '%品管%' THEN ppd.PronoteProceduresDate END AS PGJQ,
CASE WHEN wh.Workhousename LIKE '%品管%' THEN isnull(pdd.HeJiProceduresSum,0) ELSE '' END AS PGHJSCSL,
CASE WHEN wh.Workhousename LIKE '%品管%' THEN isnull(pdd.HeJiCheckOutSum,0) ELSE '' END AS PGHJHGSL,
ppd.PronoteMachineId AS JQSB,(SELECT '['+cast(pdmpro.ProductName AS varchar)+':'+ cast(isnull(pdm.PronoteQuantity,0) AS varchar) +'('+pdm.ProductUnit+')'+'],'
FROM PronotedetailsMaterial pdm LEFT JOIN Product pdmpro ON pdmpro.ProductId = pdm.ProductId WHERE pdm.PronoteHeaderID = pdd.PronoteHeaderId FOR xml path('')) AS YL,
pro.ProductDescription AS SPMS FROM PronoteHeader ph
left JOIN ProduceInDepotDetail pdd ON pdd.PronoteHeaderId = ph.PronoteHeaderId
LEFT JOIN Product pro ON pro.ProductId = pdd.ProductId
LEFT JOIN PronoteProceduresDetail ppd ON ppd.PronoteHeaderID = ph.PronoteHeaderID
LEFT JOIN WorkHouse wh ON wh.WorkHouseId = ppd.WorkHouseId
WHERE 1 = 1 AND ph.PronoteDate BETWEEN '2012-09-11' AND '2013-07-19' AND (wh.Workhousename LIKE '%射出%' OR wh.Workhousename LIKE '%品管%') and pro.ProductName is nOT NULL order by ph.PronoteDate

结果如下:

其中相同编号的数据,我只要最后一条,求大神帮忙,谢谢~
中间代码都是查询列,大家主要看第一行和from后面的就好了!

参考技术A select 主表 DISTINCT 字段1 字段2+++++ 参考技术B 在所有字段的前面,加上一个 distinct

以上是关于SQL语句多表left join SUM出现的重复数据问题!的主要内容,如果未能解决你的问题,请参考以下文章

Sql查询left join

leftjoin后,左表怎样去掉重复记录

关于SQL 查询效率问题 left join 改成 inner join union

执行sql语句,table1 left join table2 on...... 此时table2有重复的数据,从而导致最后的临时表也会出现

left join 多表关联原理

SQL Fundamentals || 多表查询(内连接,外连接(LEFT|RIGHT|FULL OUTER JOIN),自身关联,ON,USING,集合运算UNION)