我有以下 sql 查询,我的左连接没有给我明显的结果,请帮助我追踪。
SELECT DISTINCT
Position.Date,
Position.SecurityId,
Position.PurchaseLotId,
Position.InPosition,
ISNULL(ClosingPrice.Bid, Position.Mark) AS Mark
FROM
Fireball_Reporting.dbo.Reporting_DailyNAV_Pricing POSITION WITH (NOLOCK, READUNCOMMITTED)
LEFT JOIN Fireball.dbo.AdditionalSecurityPrice ClosingPrice WITH (NOLOCK, READUNCOMMITTED) ON
ClosingPrice.SecurityID = Position.PricingSecurityID AND
ClosingPrice.Date = Position.Date AND
ClosingPrice.SecurityPriceSourceID = @SourceID AND
ClosingPrice.PortfolioID IN (5,6)
WHERE
DatePurchased > @NewPositionDate AND
Position.Date = @CurrentPositionDate AND
InPosition = 1 AND
Position.PortfolioId IN (
SELECT
PARAM
FROM
Fireball_Reporting.dbo.ParseMultiValuedParameter(@PortfolioId, ',')
) AND
(
Position > 1 OR
Position < - 1
)
现在在上面我使用 LEFT JOINISNULL(ClosingPrice.Bid, Position.Mark) AS Mark
和 LEFT JOIN 时,它给了我更多没有具有多个投资组合 ID 的记录,例如。(5,6)
如果我把portfolioID =5 给出结果为120 records
如果我把portfolioID =6 给出结果为20 records
当我输入投资组合 ID = (5,6) 时,它应该给我140 records
,但它给出的结果350 records
是错误的。:(
发生这种情况是因为当我使用 LEFT JOIN 时,没有 PurchaseLotID 的条件,因为表 Fireball.dbo.AdditionalSecurityPrice ClosingPrice
没有列PurchaseLotID
,所以它给了我其他记录也有相同的 purchaseLotID 和不同的价格。
但我不想要那些记录
我怎样才能消除这些记录?