2013-01-14 45 views
0

嘿傢伙我試圖在我的postgres數據庫中運行此查詢,但它返回一個錯誤:[錯誤]錯誤:語法錯誤在或接近「,」 線13:而不是substr(a。 zoneiddest,1,3)=任何( '254', '255', '256' ...在我的SQL查詢錯誤

該查詢是這樣

SELECT 
    to_char(a.CALLDATE, 'yyyymm') AS month, 
    min(a.calldate) AS start_time, 
    max(a.calldate) AS end_time, 
    ceil(SUM(a.CALLDURATION::INT)/60) AS minutes, 
    COUNT(DISTINCT a.IDENTIFIANT) AS distinct_callers, 
    a.zoneiddest AS country_code, 
    b.country 
FROM cdr_data a, 
    country_codes b 
WHERE a.CALLSUBCLASS = '002' 
    AND a.CALLCLASS = '008' 
    AND a.zoneiddest::INT > 0 
    AND SUBSTR(a.CALLEDNUMBER, 1, 2) NOT IN 
    ('77', '78', '75', '70', '71', '41', '31', '39', '76', '79') 
    AND NOT substr(a.zoneiddest, 1, 3) = ANY 
    ('254', '255','256', '211', '257', '250', '256') 
    AND trim(a.zoneiddest) = trim(b.country_code) 
GROUP BY 
    to_char(a.CALLDATE, 'yyyymm'), 
    a.zoneiddest, 
    b.country 
ORDER BY 1 

此相同的查詢工作良好oracle中只有一小對a.zoneiddest :: integer> 0的輕微改變,只是a.zoneiddest> 0

我能做什麼錯了

回答

1

嘗試使用關鍵字的任何與參數數組結合起來是這樣的:

= any (ARRAY['254','255','256','211','257','250','256']) 
+0

是的,這也適用。非常感謝你們 – roykasa

2

問題出在您的ANY操作員身上。如果我正確理解您的查詢,您可以用NOT IN聲明替換它。

SELECT to_char (a.CALLDATE,'yyyymm') as month,min(a.calldate) as 
start_time,max(a.calldate) as end_time, 
ceil(SUM (a.CALLDURATION::integer)/60) AS minutes, 
COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers, 
a.zoneiddest as country_code,b.country 
FROM cdr_data a,COUNTRY_CODES b 
WHERE a.CALLSUBCLASS = '002' 
AND a.CALLCLASS = '008' 
and a.zoneiddest::integer > 0 
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN 
    ('77', '78', '75', '70', '71', '41', '31', '39', '76','79') 
// This line 
AND substr(a.zoneiddest , 1 ,3) NOT IN 
    ('254','255','256','211','257','250','256') 
// End of line 
and trim(a.zoneiddest) = trim(b.country_code) 
GROUP BY to_char (a.CALLDATE,'yyyymm') ,a.zoneiddest,b.country 
ORDER BY 1 
+0

謝謝。就是我想要的。 – roykasa

+0

記得標記爲正確 – jurgemaister