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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 左表数据多次出现,求解!的主要内容,如果未能解决你的问题,请参考以下文章

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

sql四种连接方式

a表 left join b表 为啥 b表里的的数据没了

MySql 之 left join 查询结果

mysql 连接

Postgresql left join左连接后数据变多去重记录(当左表1对多右表时)