查询另一个查询的结果

时间:2012-05-22 17:14:57

标签: sql group-by

这是我的第一个查询工作正常。

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))

这将给我两列,icount和yname

我想执行第二个查询,它会给我yname和icount,其中icount> 1

我已经在这几个小时了,最后决定寻求帮助。

3 个答案:

答案 0 :(得分:1)

为什么要进行第二次查询?这应该做:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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