在SQL中获取重复值

时间:2012-12-19 09:25:07

标签: sql-server-2008 inner-join

我拼命地越来越努力地完成这件事,但还没有成功。运行此查询时,我得到重复的值。

 select 
    tbl_ShipmentStatus.ShipmentID
    ,Tbl_Contract.ContractID,
    Tbl_Contract.KeyWinCountNumber,
    Tbl_Item.ItemName,
    Tbl_CountryFrom.CountryFromName,
    Tbl_CountryTo.CountryToName,
    Tbl_Brand.BrandName,
    Tbl_Count.CountName,
    Tbl_Seller.SellerName,
    Tbl_Buyer.BuyerName, 
    Tbl_Contract.ContractNumber,
    Tbl_Contract.ContractDate,
    tbl_CountDetail.TotalQty,
    tbl_CostUnit.CostUnitName,
    tbl_Comission.Payment,
    tbl_Port.PortName,
    Tbl_Contract.Vans,
    tbl_Comission.ComissionPay,
    tbl_Comission.ComissionRcv,
    tbl_CountDetail.UnitPrice,
    tbl_Comission.ComissionRemarks,
    tbl_CountDetail.Amount,
    tbl_LCStatus.LCNumber,
    tbl_ShipmentStatus.InvoiceNumber,
    tbl_ShipmentStatus.InvoiceDate,
    tbl_ShipmentStatus.BLNumber,
    tbl_ShipmentStatus.BLDate,
    tbl_ShipmentStatus.VesselName,
    tbl_ShipmentStatus.DueDate
     from tbl_ShipmentStatus 

    inner join tbl_LCStatus
    on
    tbl_LCStatus.LCID = tbl_ShipmentStatus.LCStatusID

    inner join Tbl_Contract
    on
    tbl_LCStatus.ContractID = Tbl_Contract.ContractID 

    inner join Tbl_CountDetail
    on Tbl_Contract.ContractID = Tbl_CountDetail.ContractId

    inner join tbl_Comission
    on
    tbl_Comission.ContractID = Tbl_Contract.ContractID

    inner join Tbl_Item
    on
    Tbl_Item.ItemID = Tbl_Contract.ItemID

    inner join Tbl_Brand
    on Tbl_Brand.BrandID = Tbl_Contract.BrandID
    inner join Tbl_Buyer
    on Tbl_Buyer.BuyerID = Tbl_Contract.BuyerID
    inner join Tbl_Seller
    on Tbl_Seller.SellerID = Tbl_Contract.SellerID
    inner join Tbl_CountryFrom
    on Tbl_CountryFrom.CountryFromID = Tbl_Contract.CountryFromID
    inner join Tbl_CountryTo
    on
    Tbl_CountryTo.CountryToID = Tbl_Contract.CountryToID
    inner join Tbl_Count
    on
    Tbl_Count.CountID = Tbl_CountDetail.CountId
    inner join tbl_CostUnit
    on tbl_Comission.CostUnitID = tbl_CostUnit.CostUnitID
    inner join tbl_Port
    on tbl_Port.PortID = tbl_Comission.PortID

    where tbl_LCStatus.isDeleted = 0
    and tbl_ShipmentStatus.isDeleted =0
    and tbl_LCStatus.isDeleted = 0
    and Tbl_CountDetail.isDeleted = 0
    and Tbl_Contract.isDeleted = 0

    and tbl_ShipmentStatus.LCStatusID = 5

我还附上了行结果集的图片。 任何有关为何发生这种情况的建议都会非常明显。

Result Set

2 个答案:

答案 0 :(得分:1)

通常,当您在两个表之间存在隐式部分交叉连接(笛卡尔积)时会发生这种情况。这就是我在这看起来的样子。

当你有多对多关系时,这种情况最常发生。例如,如果单个Album允许多个Artists和多个SongsArtistsSongs之间的唯一关系是Album,那么ArtistsSongs之间基本上存在多对多关系。如果您一次从所有三个表中进行选择,则会隐式地交叉加入ArtistsSongs,这可能不是您想要的。

查看您的查询,我看到Tbl_CountDetailtbl_ComissionTbl_Contract之间的多对多。尝试消除其中一个连接以进行测试,以查看该行为是否消失。

答案 1 :(得分:0)

尝试使用DISTINCT关键字。它应该解决你的问题

Select DISTINCT ....

等到我能看到你的记录不重复。

请注意CountName列和Shipment ID

每一行的组合都是独一无二的。因此,就我所见,这些价值是独一无二的。请勿选择CountName

如果你有不同的行,那么它不是重复问题。问题是在连接期间发生组合,您不希望它复制结果。

要么不选择CountName,要么您的数据有误。

这些行中只有一行应该为真6或者带有Count2或6带有Count1。同样地,对于7.当你不应该指出逻辑错误时你得到两个这样的事实