我写了以下查询
示例查询
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内的tradeamounts。我也只想查看SecurityID1值是4678和6的组合的行。该对总是有一个行为SecurityID1 4678,另一行为SecurityID1 6。
我广泛搜索了如何配对交易,但我找不到任何关于如何在+/- 5
的容差范围内允许相似值的示例答案 0 :(得分: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