表名: - 贸易
TradeNumber spread
1 .1
1 .1
2 .3
2 .4
我希望获得差价变化的所有交易。这是我的结果集应该只包含交易号2。
我可以构建的查询是
select TradeNumber from Trade t where t.spread <> (select t.spread from TradeNumber)
没有机会运行它。是否有聚合/复合函数来解决它?
答案 0 :(得分:4)
SELECT DISTINCT t.TradeNumber
FROM Trade t
WHERE EXISTS (SELECT NULL
FROM Trade t1
WHERE t.TradeNumber = t1.tradeNumber
AND t.spread <> t1.spread)
或
select TradeNumber
FROM Trade
GROUP BY TradeNumber
HAVING COUNT(DISTINCT spread) > 1
答案 1 :(得分:2)
最便宜的方法是:
select tradenumber
from t
group by tradenumber
having min(spread) <> max(spread)
使用min
和max
用户的资源少于count(distinct)
。
答案 2 :(得分:1)
为了完整性,另一种解决方案。
SELECT TradeNumber FROM (
SELECT
TradeNumber
FROM
Trade
GROUP BY TradeNumber, spread
) sq
GROUP BY TradeNumber
HAVING COUNT(*) >1
答案 3 :(得分:1)
我会选择
SELECT
TradeNumber
FROM
Trade
GROUP BY TradeNumber
HAVING count(distinct spread) > 1