Tsql 创建视图并获取重复事务

Tsql 创建视图并获取重复事务,tsql,view,outer-join,Tsql,View,Outer Join,我有以下看法: SELECT t.CompanyCode, t.FlatFileName, t.RecordNumber, t.Status, t.Branch, t.SalesMonthYear, t.SalesCashOrLimboCode, t.PageNumber, t.Driver, t.Truck, t.CommonProductCode, t.CashCharge, t.SpecialFunction, t.ProductCod

我有以下看法:

SELECT     t.CompanyCode, t.FlatFileName, t.RecordNumber, t.Status, t.Branch, t.SalesMonthYear, t.SalesCashOrLimboCode, t.PageNumber, t.Driver, t.Truck, 
                      t.CommonProductCode, t.CashCharge, t.SpecialFunction, t.ProductCode, t.UnitOfEntry, t.TransactionBranch, t.AccountNumber, t.ReferenceNumber, t.TransactionDate, 
                      t.PercentFullOrWhenDueCode, t.Quantity, t.DeliveryAccountNumberForTankRentals, t.Amount, t.SalesTax, t.ProductCode2Amount, t.TankSerialNumber, t.SalesMonth, 
                      t.TaxCode, t.SubjectToTax, t.SalesTaxRate, t.ExiseTaxRate, t.FrankHasntDecidedWhatThisFieldIs_ProbablyCentury, t.OriginalSalesMonth, t.AutoCharge, 
                      t.ProductCode2, t.ProductCode2SpecialFunction, t.DiscountCode, t.DiscountRate, t.DiscountDays, t.Century, t.TRFRPD, t.OpenItemAmountPaid, 
                      t.OpenItemReferenceNumber, t.InvoiceFlag, t.InvoiceNumber, t.Time, t.GasQuantity, t.AnodeReading, t.Reading, t.Reason, t.InventorySerialNumber, t.SName, 
                      t.ErrorCode, t.MasterBillingBranchAccountNumber, t.LeaseOnFile, t.PuchaseOrderNumber, t.BillOfLaden, t.Latitude, t.Longitude, t.ContractGasPrice, t.Balance, 
                      t.DeliveryAccount, t.BranchAccountNumber, t.LineNumber, t.DateTimeEntered, t.UserThatEntered, t.WorkstationThatEntered, t.YearMonthDayPosted, 
                      t.HourMinutePosted, t.UserThatPosted, t.WorkstationThatPosted, t.CreditCardNumber, t.CreditCardExperationDate, t.CreditViolation, t.TransactionId, t.CorporationId, 
                      t.IsUpdated, t.IsAdded, t.ClearUpdateAndAdded, p.Description
FROM         Company.Product AS p LEFT OUTER JOIN
                      Company.[Transaction] AS t ON p.ProductCode = t.ProductCode AND p.CorporationId = t.CorporationId
我试图完成的主要任务是从产品表中获取产品的描述,并将其添加到事务表中。它必须基于产品代码

我应该得到每个交易只有一次,但我得到了每个交易的多个副本。我知道每个公司都有这些产品,所以我想这是因为看到了这些

由此可知,它表示使用左外连接:

此联接返回左表中与 右表中的匹配行。如果没有列 在右表中匹配,它返回空值


我在描述中没有得到空值,因此我不知道我做错了什么。

您可能希望加入产品上的事务,因为每个产品都有多个事务。我会交换联接中的订单,也不会在外部联接中使用,因为在您的场景中,您希望查询的所有事务都是1-1存在的(即没有产品的事务)

我会更改表的顺序:

SELECT  <yourcols>
FROM   company.[transaction] AS t 
LEFT OUTER JOIN company.product AS p 
   ON t.productcode = p.productcode
   AND t.corporationid = p.corporationid 
选择
来自公司。[交易]作为t
左外联接公司。产品为p
关于t.productcode=p.productcode
t.corporationid=p.corporationid

我假设您有一对多关系,即对于一笔交易,您有许多产品。不是吗?每个产品代码有多个CorporationID吗?这可能导致这种情况。上述查询的左外联接不会在描述中为空;它将在公司表的所有字段中为空。这是真的吗?如果向SELECT语句添加DISTINCT,是否会更改返回的行数?如果是这样,你就得到了真实的副本;否则,有些数据是不同的。