Sql Sybase对不同行集的行数

时间:2011-04-26 18:59:10

标签: sql sybase

我有一个表,其中包含每笔交易的security_id和side(买入或卖出)。我想计算不同的security_id和side组合的数量。

因此,当我执行以下操作时,我会获得所有符号方面的列表,但我不知道如何获得总计数?

select 
  security_id, side
from 
  trade
group by security_id, side

我想要的结果是1行,告诉我表中有多少不同的security_id,我有。

还有一种方法可以确定哪些security_id有双方的条目(买入和卖出)。

2 个答案:

答案 0 :(得分:2)

Select Count(*)
From    (
        Select security_id, side
        From trade
        Group security_id, side
        ) As Z

第二个问题:

Select security_id
From trade
Where side In('BUY','SELL')
Group security_id
Having Count( Distinct side ) = 2

答案 1 :(得分:0)

你试过这个吗?

select 
 security_id, side , count(*)
     from 
        trade
       group by security_id, side