简体   繁体   English

加入返回的重复项

[英]Join returning duplicates

I keep getting duplicates on my join statement , any join experts please help I have been scratching my head for hours 我的加入声明中不断出现重复,任何加入专家请帮助我数小时不停地挠头

SELECT  
  DISTINCT  
  image.ImageID,
  Lotpw.Lot,
  lot.Oper,
  lot.Product,
  lot.Process,
  Image.PartialWafer,
  image.UnitX,
  image.UnitY,
  Image.InspectionStation,
  Image.Status,
  Image.ImagePath,
  image.Date,
FROM Lot 
--FROM [VueDb].[dbo].[Lot] join [VueDb].[dbo].[LotPW] on lot.Lot=lotpw.Lot join [VueDb].[dbo].[Image] on lotpw.PartialWafer = image.PartialWafer 
  LEFT JOIN LotPW 
    on lot.Lot = lotpw.Lot  
  RIGHT JOIN image 
    on LOTPW.partialwafer = Image.partialwafer

WHERE (@Lot= '' or  Lot.Lot= @Lot)
  and (@Oper ='' or Lot.Oper= @Oper)
  and (@Product= '' or  Lot.Product= @Product)
  and (@Process= '' or Lot.Process= @Process)
  and (@PartialWafer= '' or  image.PartialWafer= @PartialWafer)
  and (@UnitX ='' or Image.UnitX= @UnitX)
  and (@UnitY= '' or Image.UnitY= @UnitY)
  and (@InspectionStation ='' or Image.InspectionStation = @InspectionStation)
  and (@Status ='' or Image.Status= @Status)
  and (@LossCode= '' or Image.LossCode= @LossCode)

在此处输入图片说明

below is an image of the output 下面是输出的图像

(Not a solution but might help) (不是解决方案,但可能会有所帮助)

Try this like Philip suggests and then inspect the data: 像Philip建议的那样尝试一下,然后检查数据:

SELECT  
  *
FROM Lot 
  LEFT JOIN LotPW 
    on lot.Lot = lotpw.Lot  
  RIGHT JOIN image 
    on LOTPW.partialwafer = Image.partialwafer

I'm guessing you need to add other conditions into your ON clauses. 我猜您需要在ON子句中添加其他条件。

From the way your query is setup, it looks like Lot is the top of the hierarchy, and has a 1:n relationship with LotPW (LotPW.Lot is suggesting the column:Lot is the PK on table:Lot, and thus the column:Lot is a FK on LotPW). 从查询的设置方式来看,Lot似乎是层次结构的顶部,并且与LotPW具有1:n的关系(LotPW.Lot建议column:Lot是table:Lot上的PK,因此该列:Lot是LotPW上的FK)。 The left join suggest that you don't think each Lot row has a LotPW row (child) in the database. 左联接表明您不认为数据库中每个Lot行都有一个LotPW行(子级)。 This join will produce multiple rows any time there is more than 1 row in Lot which matches with LowPW. 只要Lot中有超过1行与LowPW相匹配,此连接就会产生多行。

The join to Image suggests that LotPW is the parent in this relationship, and that LotPW.PartialWafer is the PK on the LotPW table. 与Image的联接表明,LotPW是此关系中的父级,而LotPW.PartialWafer是LotPW表上的PK。 With this join being a Right Join to LotPW, it's saying that you don't believe that each PW has a parent. 由于此联接是对LotPW的正确联接,这意味着您不相信每个PW都有父级。 This right Join should probably be a left join, otherwise you're negating the Left Join between Lot and LotPW 此右联接可能应该是左联接,否则您将否定Lot和LotPW之间的左联接

To de-dupe this, assuming multiple rows may be present in Lot or LotPW which match to a given PW, but you don't care about the extra data, do the join between Lot and LotPW and Group By on all values. 为了消除这种重复,假设Lot或LotPW中可能存在与给定PW匹配的多行,但是您不在乎多余的数据,请对所有值进行Lot和LotPW以及Group By之间的联接。 Then use this resultset in the Left/Right Join with PW. 然后在“与PW的左/右连接”中使用此结果集。

something like: 就像是:

     Select distinct
        image.ImageID,
        Lotpw.Lot,
        lot.Oper,
        lot.Product,
        lot.Process,
        Image.PartialWafer,
        image.UnitX,
        image.UnitY,
        Image.InspectionStation,
        Image.Status,
        Image.ImagePath,
        image.Date
     From 
        (select Lot.Lot, Lot.Oper, lot.Product, lot.process, LotPW.PartialWafer 
            From Lot left join LotPW on Lot.Lot = LotPW.Lot
            Group by Lot.Lot, Lot.Oper, lot.Product, lot.process) UniqueLots
       Left Join Image on Image.PartialWafer = UniqueLots.PartialWafer

Every row in your list is distinct; 列表中的每一行都是不同的; select distinct is a row operator meaning it works across the entire row . select distinctrow operator这意味着它适用于整个行 Notice (in yellow) that every row does have something different. 注意(黄色),每一行的确有所不同。 pxq100 != pxq200 and so on. pxq100!= pxq200,依此类推。

在此处输入图片说明

So, if you have used select distinct and you think you still think you are getting duplicates then you just haven't used the right technique because select distinct only does one thing , you need GROUP BY or ROW_NUMBER() 因此,如果您使用了select distinct并且您认为仍然认为自己正在获得重复项,那么您就没有使用正确的技术,因为select different只做一件事情 ,您需要GROUP BYROW_NUMBER()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM