1

表名: - 贸易

TradeNumber       spread
1                 .1
1                 .1
2                 .3
2                 .4

我想获得点差不同的所有交易。那就是我的结果集应该只包含交易号 2。

我可以构造的查询是

select TradeNumber from Trade t where t.spread <> (select t.spread from TradeNumber)

没有机会运行它。是否有聚合/复合函数来解决它?

4

4 回答 4

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

sqlFiddle

于 2013-01-24T14:13:11.320 回答
2

最便宜的方法是:

select tradenumber
from t
group by tradenumber
having min(spread) <> max(spread)

使用minmax用户的资源少于count(distinct).

于 2013-01-24T14:25:33.120 回答
1

只是为了完整性,另一种解决方案。

SELECT TradeNumber FROM (
SELECT
TradeNumber
FROM
Trade
GROUP BY TradeNumber, spread
) sq
GROUP BY TradeNumber
HAVING COUNT(*) >1
于 2013-01-24T14:13:58.983 回答
1

我会去

SELECT
TradeNumber
FROM
Trade
GROUP BY TradeNumber
HAVING count(distinct spread) > 1
于 2013-01-24T14:21:24.360 回答