2013-07-29 4 views
0

Ce qui suit est mon codeNombre conditionnel dans SQL

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 

Ce que je veux faire est d'afficher uniquement le nombre de a.isclanmatch> = 2. C'est possible?

Répondre

3

Ajouter

HAVING COUNT(a.isclanmatch)>=2 

à la fin de votre requête

+0

cela fonctionne parfaitement! Merci! – user2129608

2

Je pense que vous voulez faire:

HAVING COUNT(a.isclanmatch)>=2 
-1

Si l'on suppose votre requête en cours est très bien, cela devrait fonctionner

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 
+0

Pourquoi complexer une simple requête? – Romesh

+0

Ouais, d'accord c'est assez simple. Je ne dis pas que l'ajout d'avoir ne fonctionne pas, mais c'est une approche alternative et plus flexible dans les cas où il veut sélectionner seulement un sous-ensemble (quelques colonnes) du résultat, faire l'ordre par groupe ou autres. –