访问查询返回重复记录

时间:2014-03-10 19:45:08

标签: sql ms-access join

我有一个我正在使用Access的查询,它应该获取另一个查询中指定的记录并更改它们。不幸的是,如果我之前选择了多个记录,它会在这个新查询中创建重复项。

例如,如果我选择了2条记录,则会为每条记录创建两条相同的条目。如果我有3个被选中,它会为每个创建3个记录,当我只想要3时总共有9个记录。如果我最初只有一个记录它完美地工作。

我读到它可能是连接的问题,但我不确定如何解决它。

以下是我的代码,我希望我能够很好地解释自己:/

   SELECT 
      GV_transfer3.[Dept ID], 
      GV_transfer3.[Existing Account], 
      GV_transfer3.Class,     
      GV_transfer3.Fund, 
      GV_transfer3.Program, 
      GV_transfer3.Project, 
      GV_transfer3.ID, 
      GV_transfer3.[project Number], 
      GV_transfer3.[Account Number], 
      GV_transfer3.Code, 
      GV_transfer3.Date, 
      GV_transfer3.Vendor, 
      'transferred from ' & Right([GV_transfer3].[Project Number],Len([GV_transfer3].[Project Number])-8) & ' to ' & Right([New Project Number],Len([New Project Number])-8) & '; ' & [GV_transfer3].[Description] AS Description1, 
      GV_transfer3.[Req By], 
      GV_transfer3.[Approved By], 
      GV_transfer3.[Proj# Number], 
      GV_transfer3.[Transferred out], 
      GV_transfer.Action, 
      -[Amount to transfer] AS Amount, 
      0 AS Reconciled, 
      'done ' & (Date()) & '; ' & [amount to transfer] & ' from ' & Right([GV_transfer3].[Project Number],Len([GV_transfer3].[Project Number])-8) & ' to ' & Right([New Project Number],Len([New Project Number])-8) & '; ' & [GV_transfer3].[Comment] AS Comment1, 
      GV_transfer3.Transfer, 
      GV_transfer3.Match, 
      IIf((Date())<=#6/30/2010#,'FY10',IIf((Date()) Between #7/1/2010# And #6/30/2011#,'FY11',IIf((Date()) Between #7/1/2011# And #6/30/2012#,'FY12','FY13'))) AS [Fiscal Year], 
      GV_transfer3.EquipGroupID, 
      GV_transfer3.EquipNumber, 
      GV_transfer3.Rep_Maint_Purchase, 
      Null AS Budget, GV_transfer.[Rel Project], 
      GV_transfer.MEIF, 
      GV_transfer.Released, 
      GV_transfer3.Proposed, GV_transfer3.Funded, 
      GV_transfer3.Declined, 
      GV_transfer3.Indirect, 
      GV_transfer3.DIC, 
      GV_transfer3.Forecast, 
      GV_transfer3.IntFunded, 
      GV_transfer3.Invoice, 
      GV_transfer3.VContract, 
      GV_transfer3.Category, 
      GV_transfer3.Activity
    FROM GV_transfer3 
    INNER JOIN GV_transfer 
    ON GV_transfer3.ID = GV_transfer.ID;

3 个答案:

答案 0 :(得分:1)

您可以尝试使用DISTINCT命令。在访问中使用SQL。 它将消除列/列中的重复。 创建一个新查询并选择您所做的当前查询,以便复制。

SELECT DISTINCT(“列名”) FROM“表/查询名称”;

这将使新查询消除所有重复。 希望有所帮助。

答案 1 :(得分:0)

这只是猜测 - 但看起来您在GV_transferGV_transfer3或两者中都有相同ID的重复行。您需要确保两个表都使用唯一ID。检查的一种方法是逐个对表运行以下查询:

select ID, count(ID) as num_times
from GV_transfer
group by ID

...和GV_transfer3的相同查询,表名已修改。

答案 2 :(得分:0)

我尝试使用LEFT JOIN而不是INNER JOIN。