Consultar resultados de otra consulta

Here is my first query that works fine.

SELECT count(event_log_id) as icount, cast(youth_name as varchar(max)) as yname
FROM CIRComplete
WHERE  actual_date between '2012-01-01' and '2012-04-30'
  AND is_deleted = '0' AND Closed = '1' 
GROUP BY cast(youth_name as varchar(max))

This will give me two columns, icount and yname

I want to perform a second query that will give me yname and icount where icount > 1

I have been at this for hours now and have finally decided to ask for help.

preguntado el 22 de mayo de 12 a las 18:05

3 Respuestas

Why a second query? This should do:

SELECT
   count(event_log_id) as icount ,
   cast(youth_name as varchar(max)) as yname 
   FROM CIRComplete 
   WHERE (actual_date between '2012-01-01' and '2012-04-30') and 
         is_deleted = '0' and Closed = '1' 
   GROUP BY cast(youth_name as varchar(max))
   HAVING count(event_log_id) > 1

contestado el 22 de mayo de 12 a las 18:05

Thank you all for the quick response. I totally forgot about the Having clause. - Esteban Hathaway

SELECT  cast(youth_name as varchar(max)) as yname,
        count(event_log_id) as icount
FROM    CIRComplete
WHERE   (actual_date between '2012-01-01' AND '2012-04-30') AND 
        is_deleted = '0' AND 
        Closed = '1' 
GROUP BY cast(youth_name as varchar(max))
HAVING  count(event_log_id) > 1

contestado el 22 de mayo de 12 a las 18:05

SELECT  
   count(event_log_id) as icount
  ,cast(youth_name as varchar(max)) as yname
FROM
  CIRComplete
WHERE 
   (actual_date between '2012-01-01' and '2012-04-30') and is_deleted = '0' and Closed = '1' 
GROUP BY
   cast(youth_name as varchar(max))
having icount > 1

contestado el 22 de mayo de 12 a las 18:05

Is using an alias in the having clause really permissible? - Mithrandir

Depends on the database, but this is non-standard and uncommon SQL. - gordon linoff

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.