使用SQL查找相似的数据对

使用SQL查找相似的数据对,sql,join,Sql,Join,我编写了以下查询 SecurityID PortfolioCode SymbolNameSpace Symbol TradeAmount tradedate settledate PostDate TransactionCode SecurityID1 4678 pendingtest caus pending 368,456.00 5/21/2013 NULL 5/21/2013 lo 4678 6 pendingtest caus

我编写了以下查询

SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
6   pendingtest caus    cash     240,453.70     7/16/2010   NULL    7/19/2010   lo  6
6   pendingtest caus    cash     249,562.32     1/19/2012   1/19/2012   1/20/2012   dp  6
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
6   pendingtest caus    cash     250,000.00     12/1/2003   NULL    12/1/2003   lo  6
6   pendingtest caus    cash     250,321.13     11/15/2010  11/15/2010  11/16/2010  dp  6
6   pendingtest caus    cash     365,445.58     1/31/1999   NULL    3/26/1999   dp  6
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
示例查询

SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
6   pendingtest caus    cash     240,453.70     7/16/2010   NULL    7/19/2010   lo  6
6   pendingtest caus    cash     249,562.32     1/19/2012   1/19/2012   1/20/2012   dp  6
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
6   pendingtest caus    cash     250,000.00     12/1/2003   NULL    12/1/2003   lo  6
6   pendingtest caus    cash     250,321.13     11/15/2010  11/15/2010  11/16/2010  dp  6
6   pendingtest caus    cash     365,445.58     1/31/1999   NULL    3/26/1999   dp  6
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
期望结果集

SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
6   pendingtest caus    cash     240,453.70     7/16/2010   NULL    7/19/2010   lo  6
6   pendingtest caus    cash     249,562.32     1/19/2012   1/19/2012   1/20/2012   dp  6
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
6   pendingtest caus    cash     250,000.00     12/1/2003   NULL    12/1/2003   lo  6
6   pendingtest caus    cash     250,321.13     11/15/2010  11/15/2010  11/16/2010  dp  6
6   pendingtest caus    cash     365,445.58     1/31/1999   NULL    3/26/1999   dp  6
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
样本实际结果集

SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
6   pendingtest caus    cash     240,453.70     7/16/2010   NULL    7/19/2010   lo  6
6   pendingtest caus    cash     249,562.32     1/19/2012   1/19/2012   1/20/2012   dp  6
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
6   pendingtest caus    cash     250,000.00     12/1/2003   NULL    12/1/2003   lo  6
6   pendingtest caus    cash     250,321.13     11/15/2010  11/15/2010  11/16/2010  dp  6
6   pendingtest caus    cash     365,445.58     1/31/1999   NULL    3/26/1999   dp  6
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
逻辑是,我只想看到表中的行对与+/-5内的交易金额匹配。我还只想看到SecurityID1值是4678和6的组合的行。该对将始终有一行为SecurityID1 4678,另一行为SecurityID1 6

SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
6   pendingtest caus    cash     240,453.70     7/16/2010   NULL    7/19/2010   lo  6
6   pendingtest caus    cash     249,562.32     1/19/2012   1/19/2012   1/20/2012   dp  6
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
6   pendingtest caus    cash     250,000.00     12/1/2003   NULL    12/1/2003   lo  6
6   pendingtest caus    cash     250,321.13     11/15/2010  11/15/2010  11/16/2010  dp  6
6   pendingtest caus    cash     365,445.58     1/31/1999   NULL    3/26/1999   dp  6
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
我广泛搜索了如何对事务进行配对,但我找不到任何示例,说明如何允许在+/-5公差范围内使用类似值。如果您不介意在结果中将两个配对事务放在同一行而不是单独的行上,那么您可以将每个集合创建为派生表,例如下面的a和b。然后他们就这样

SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SecurityID  PortfolioCode   SymbolNameSpace Symbol  TradeAmount tradedate   settledate  PostDate    TransactionCode SecurityID1
6   pendingtest caus    cash     240,453.70     7/16/2010   NULL    7/19/2010   lo  6
6   pendingtest caus    cash     249,562.32     1/19/2012   1/19/2012   1/20/2012   dp  6
6   pendingtest caus    cash     368,456.15     5/22/2013   NULL    5/23/2013   lo  6
6   pendingtest caus    cash     250,000.00     12/1/2003   NULL    12/1/2003   lo  6
6   pendingtest caus    cash     250,321.13     11/15/2010  11/15/2010  11/16/2010  dp  6
6   pendingtest caus    cash     365,445.58     1/31/1999   NULL    3/26/1999   dp  6
4678    pendingtest caus    pending  368,456.00     5/21/2013   NULL    5/21/2013   lo  4678
4678    pendingtest caus    pending  7,800.00   4/17/2013   NULL    5/21/2013   lo  4678
6   pendingtest caus    cash     7,801.00   4/23/2013   NULL    5/23/2013   lo  6
SELECT 
a.SecurityID SecurityID_a, a.PortfolioCode PortfolioCode_a, a.SymbolNameSpace SymbolNameSpace_a, a.Symbol Symbol_a, a.TradeAmount TradeAmount_a, a.tradedate TradeDate_a, a.settledate SettleDate_a, a.PostDate PostDate_a, a.TransactionCode TransactionCode_a, a.SecurityID1 SecurityID1_a,
b.SecurityID SecurityID_b, b.PortfolioCode PortfolioCode_b, b.SymbolNameSpace SymbolNameSpace_b, b.Symbol Symbol_b, b.TradeAmount TradeAmount_b, b.tradedate TradeDate_b, b.settledate SettleDate_b, b.PostDate PostDate_b, b.TransactionCode TransactionCode_b, b.SecurityID1 SecurityID1_b 
FROM 
(
  SELECT Sec.SecurityID, Port.PortfolioCode, Sec.SymbolNameSpace, Sec.Symbol, PT.TradeAmount, PT.tradedate, PT.settledate, PT.PostDate, PT.TransactionCode, PT.SecurityID1
  FROM AdvApp.vPortfolioTransaction PT
  JOIN AdvApp.vSecurity Sec ON Sec.SecurityID = PT.SecurityID1
  LEFT JOIN AdvApp.vPortfolio PORT ON Port.PortfolioID = PT.PortfolioID
  WHERE Sec.SecurityID = 4678 
) a
CROSS JOIN
(
  SELECT Sec.SecurityID, Port.PortfolioCode, Sec.SymbolNameSpace, Sec.Symbol, PT.TradeAmount, PT.tradedate, PT.settledate, PT.PostDate, PT.TransactionCode, PT.SecurityID1
  FROM AdvApp.vPortfolioTransaction PT
  JOIN AdvApp.vSecurity Sec ON Sec.SecurityID = PT.SecurityID1
  LEFT JOIN AdvApp.vPortfolio PORT ON Port.PortfolioID = PT.PortfolioID
  WHERE Sec.SecurityID = 6 
) b 
WHERE ABS(a.TradeAmount - b.TradeAmount) <= 5 

谢谢,这是非常接近,但提供了太多的信息。如果我包含和a.PortfolioCode=b.PortfolioCode,查询的效果会更好,但这只显示了一行。理想情况下,我希望看到两行。还有一行显示的是Symbol_a=pending所在的行。我还想看看符号=cash的那一行,在我添加And子句之后,我在一行上获得了所有信息。在两条线上看到它有多复杂?忽略前面的评论我没有注意到它都出现在一行上。我认为你不应该添加和。返回的数据项与所需的结果相同,只是对在一行中,而不是单独的行中。对我来说,这种方式似乎更自然,因为您不依赖于按顺序查看成对,因为它们并排在同一行中。您只需使用列别名来区分它们。如果我不包括,并且结果集太大。在许多情况下,tradeamount在5个公差范围内,但端口a和端口b不相同,因此会出现误报。使用并只给出真正的积极因素。一种解决方法是包含两个单独的查询。一个只选择端口a列,另一个使用端口b列。然后我可以在SSRS报告中使用这两个数据集作为我的最终目标,但如果我可以在一个查询中完成这一切,那就太好了。