2

我写了以下查询

示例查询

USE SampleDB
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
LEFT OUTER JOIN
    AdvApp.vPortfolio PORT
    ON Port.PortfolioID = PT.PortfolioID
LEFT OUTER JOIN
    AdvApp.vSecurity Sec
    ON Sec.SecurityID = PT.SecurityID1
WHERE Sec.SecurityID = 4678 or Sec.SecurityID = 6
ORDER BY PT.TradeAmount

期望的结果集

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。

我广泛搜索了如何配对交易,但我找不到任何示例说明如何在 +/- 5 的容差范围内允许类似值

4

1 回答 1

0

如果您不介意将两个配对事务放在结果中的同一行而不是单独的行,那么您可以将每个集合创建为派生表,例如下面的a&命名b。然后CROSS JOIN他们就这样...

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 
于 2013-05-24T08:55:33.520 回答