加入返回的重复项

时间:2014-08-22 19:52:24

标签: sql join

我在加入声明中不断重复,任何加入专家请帮助我多次抓挠我的脑袋

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)

enter image description here

下面的

是输出的图像

3 个答案:

答案 0 :(得分:0)

(不是解决方案,但可能有帮助)

尝试像菲利普建议的那样,然后检查数据:

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

我猜你需要在ON条款中添加其他条件。

答案 1 :(得分:0)

从您的查询设置方式来看,Lot似乎是层次结构的顶层,并且与LotPW具有1:n的关系(LotPW.Lot建议列:Lot是桌面上的PK:Lot,和因此列:Lot是LotPW上的FK)。左连接表明您不认为每个Lot行在数据库中都有LotPW行(子)。每当Lot中有超过1行与LowPW匹配时,此连接将产生多行。

对图像的连接表明LotPW是此关系中的父级,而LotPW.PartialWafer是LotPW表上的PK。由于此连接是对LotPW的正确加入,它表示您不相信每个PW都有父级。这个右连接可能应该是左连接,否则你否定Lot和LotPW之间的左连接

要对此进行重复数据删除,假设Lot或LotPW中可能存在与给定PW匹配的多行,但您不关心额外数据,请在Lot和LotPW之间进行连接并对所有值进行分组。然后在左/右连接PW中使用此结果集。

类似的东西:

     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

答案 2 :(得分:0)

列表中的每一行都是不同的; select distinctrow operator,意味着它适用于整行。请注意(黄色)每行确实有不同的东西。 pxq100!= pxq200等等。

enter image description here

所以,如果您使用过select distinct,并且您认为自己仍然认为自己正在使用重复项,那么您只能使用正确的技术,因为select distinct only does one thing,您需要GROUP BYROW_NUMBER()

相关问题