简体   繁体   中英

Error in my sql query

Hey guys i am trying to run this query in my postgres db but it returns an error: [Err] ERROR: syntax error at or near "," LINE 13: and not substr(a.zoneiddest , 1 ,3) = any ('254','255','256'...

The query is like this

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

This same query works well in oracle with just a small minor change on a.zoneiddest::integer > 0 to just a.zoneiddest > 0

What could i be doing wrong

The problem is with your ANY operator. If i understand your query correctly, you can just substitute it with a NOT IN statement.

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

尝试将关键字any与参数数组结合使用,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM