2012-05-22 209 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。 –

相關問題