选择语句以在列中查找变量数据

时间:2013-01-24 14:09:13

标签: sql sybase

表名: - 贸易

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 个答案:

答案 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

sqlFiddle

答案 1 :(得分:2)

最便宜的方法是:

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

使用minmax用户的资源少于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