我有一个运行良好的比较查询,但我有许多结果,我认为应该匹配的价格出现在不同的行中。有什么好方法可以让他们匹配给定的代码和帐户,以便将他们排除在我的结果集中吗?
询问:
with cteDev3 (Sequence, AccountID, SecurityID, Ticker, UniqueAccountID, BuyPrice, BuyDate, Shares)as
(select Al.Sequence, Al.AccountID, al.SecurityID, s.Ticker,ad.UniqueAccountId, BuyPrice, BuyDate, Shares
from dev3TM2.dbo.AccountLot al
join dev3TM2.dbo.AccountDetails ad
on al.AccountID = ad.AccountId
join dev3TM2.dbo.Security s
on al.SecurityId = s.SecurityId
where ad.EnterpriseId = 'HuberFinancial'),
cteTest2 (Sequence, AccountID, SecurityID,Ticker, UniqueAccountID, BuyPrice, BuyDate, Shares)as
(select Al.Sequence, Al.AccountID, al.SecurityID, s.Ticker,ad.UniqueAccountId, BuyPrice, BuyDate, Shares
from test2TM2.dbo.AccountLot al
join test2TM2.dbo.AccountDetails ad
on al.AccountID = ad.AccountId
join Test2TM2.dbo.Security s
on al.SecurityId = s.SecurityId
where ad.EnterpriseId = 'HuberFinancial')
select c3.UniqueAccountId as 'Dev3', t2.UniqueAccountId as 'Test2',c3.Ticker, t2.Ticker,
c3.Shares,t2.Shares, c3.BuyDate, t2.BuyDate, c3.BuyPrice as 'Dev3_BuyPrice', t2.BuyPrice as 'Test2_BuyPrice'
from cteTest2 t2 full outer join
cteDev3 c3
on c3.UniqueAccountId = t2.UniqueAccountId
where
--c3.UniqueAccountID is null or t2.UniqueAccountID is null and
c3.SecurityID = t2.SecurityID and
c3.BuyDate = t2.BuyDate and
c3.Shares = t2.Shares and
(c3.BuyPrice <= t2.BuyPrice -0.001 or c3.BuyPrice >=t2.BuyPrice + 0.001)
--c3.BuyPrice = t2.BuyPrice
order by c3.UniqueAccountID, t2.UniqueAccountID, c3.Ticker, t2.Ticker, c3.BuyDate, t2.BuyDate,
c3.BuyPrice, t2.BuyPrice
结果集(部分):
Dev3 | Test2 | Ticker | Ticker | Shares | Shares | BuyDate | BuyDate | Dev3_BuyPrice | Test2_BuyPrice
10735975 | 10735975 | TLM | TLM | 100.000000 | 100.000000 | 2012-01-12 00:00:00 | 2012-01-12 00:00:00 | 11.859000 | 11.860000
10735975 | 10735975 | TLM | TLM | 100.000000 | 100.000000 | 2012-01-12 00:00:00 | 2012-01-12 00:00:00 | 11.860000 | 11.859000
11123259 | 11123259 | EDC | EDC | 0.297700 | 0.297700 | 2010-03-31 00:00:00 | 2010-03-31 00:00:00 | 163.329750 | 163.318777
11123259 | 11123259 | EDC | EDC | 0.337700 | 0.337700 | 2010-06-30 00:00:00 | 2010-06-30 00:00:00 | 110.489221 | 110.482677
11123259 | 11123259 | EDC | EDC | 1.379500 | 1.379500 | 2011-03-30 00:00:00 | 2011-03-30 00:00:00 | 201.048191 | 201.051105
11123259 | 11123259 | EDC | EDC | 0.023500 | 0.023500 | 2011-03-30 00:00:00 | 2011-03-30 00:00:00 | 201.105442 | 201.276596
11123259 | 11123259 | EDC | EDC | 1.022900 | 1.022900 | 2011-06-29 00:00:00 | 2011-06-29 00:00:00 | 179.398940 | 179.391925
11123259 | 11123259 | EDC | EDC | 0.063500 | 0.063500 | 2011-06-29 00:00:00 | 2011-06-29 00:00:00 | 179.414542 | 179.527559