我的表格结构如下:
TradeNo | OrderNo | Buy_Sell | Trade_Qty | Market_Rate | Sauda_Date | Expiry_Date
1001 2001 Buy 100 5000 28 Feb 2013 2013-03-29
1002 2002 Buy 500 8000 28 Feb 2013 2013-03-29
1001 2001 Sell 70 5600 1 Mar 2013 2013-03-29
每行TradeNo和OrderNo是唯一的组合。
我想选择具有唯一TradeNo和OrderNo组合的所有记录[如第1和第3],其中Sauda_Date可能不同但在Expirydate内且根据买/卖。
从此示例中,应该选择以下行:
TradeNo | OrderNo | Buy_Sell | Trade_Qty | Market_Rate | Sauda_Date | Expiry_Date
1001 2001 Buy 100 5000 28 Feb 2013 2013-03-29
1001 2001 Sell 70 5600 1 Mar 2013 2013-03-29
选择这些行是因为Tradeno和OrderNo对于这些行是唯一的,并且根据买卖和sauda日期在到期日期内。
我尝试过以下查询,但不起作用:
select
t1.TradeNo,
t1.OrderNo,
t1.Trade_Qty,
t1.Market_Rate,
t1.Sauda_Date,
t1.Expirydate
from
tradeFile t1,
tradeFile t2
where
t1.TradeNo=t2.TradeNo and
t1.OrderNo=t2.OrderNo
请帮帮我。
答案 0 :(得分:1)
请改为尝试:
SELECT t1.*
FROM tradeFile t1
INNER JOIN
(
SELECT TradeNo, OrderNO
FROM tradeFile
GROUP BY TradeNo, OrderNO
HAVING COUNT(*) > 1
) AS t2 ON t1.TradeNo = t2.TradeNo
AND t1.OrderNo = t2.OrderNO;