2012-05-22 210 views
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)) 

这将给我两列,ICOUNT和yname

我想执行,这将使我yname和第二查询ICOUNT其中ICOUNT> 1

我一直在这几个小时现在终于决定寻求帮助。

回答

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

谢谢大家的快速反应。我完全忘记了Having条款。 –

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

在having条款中使用别名是否允许使用? – Mithrandir

+0

取决于数据库,但这是非标准且不常见的SQL。 –

相关问题