SQL中的条件计数

时间:2013-07-29 08:41:08

标签: sql aggregate-functions having-clause

以下是我的代码

SELECT b.fulldate, 
       b.userid, 
       Count(a.isclanmatch) 
FROM  (SELECT fulldate, 
              realmatchid, 
              isclanmatch 
       FROM   gro_int.int_match 
       WHERE  ( fulldate BETWEEN '2013-06-30' AND Now() - 2 ) 
              AND isclanmatch = 1 
       GROUP  BY realmatchid)a 
      INNER JOIN gro_int.int_match_user b 
              ON b.realmatchid = a.realmatchid 
WHERE  ( b.fulldate BETWEEN '2013-06-30' AND Now() - 2 ) 
GROUP  BY userid 

fulldate    userid  count(a.isclanmatch)
2013-07-09  1417    4
2013-07-15  1581    2
2013-06-30  1603    1

我想要做的只是显示a.isclanmatch> = 2的计数。可能吗?

3 个答案:

答案 0 :(得分:3)

添加

HAVING COUNT(a.isclanmatch)>=2

到查询结尾

答案 1 :(得分:2)

我想你想做:

HAVING COUNT(a.isclanmatch)>=2

答案 2 :(得分:-1)

假设您当前的查询没问题,这应该可以正常工作

WITH mycte as(
SELECT b.fulldate, b.userid, COUNT(a.isclanmatch) colname
FROM(
    SELECT fulldate, realmatchid, isclanmatch
    FROM gro_int.int_match
    WHERE (fulldate BETWEEN '2013-06-30' AND NOW()-2) AND isclanmatch = 1
    GROUP BY realmatchid)a
        INNER JOIN gro_int.int_match_user b
    ON b.realmatchid = a.realmatchid
    WHERE (b.fulldate BETWEEN '2013-06-30' AND NOW()-2)
GROUP BY userid
)
select * from mycte where colname >= 2