sql - 我的sql查询出错

标签 sql postgresql

嘿伙计们,我试图在我的 postgres 数据库中运行这个查询,但它返回一个错误:[Err] 错误:“,”或附近的语法错误 第 13 行:and not substr(a.zoneiddest , 1 ,3) = any ('254','255','256'...

查询是这样的

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

我哪里做错了

最佳答案

问题出在您的 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

关于sql - 我的sql查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14316759/

相关文章:

mysql - 我正在尝试将数据从 table1 填充到 table2。我的 sql 查询是否正确?

MySQL Unknown column on 子句

在同一条目上并行更新的 Mysql 锁(使用 sidekiq)

sql-server - Debezium 服务器到 PubSub : delete-operations breaks the application

java - POSTGRES (EclipseLink) 中的 JSON 数据存储

postgresql - 如何在 postgres 全文搜索中使用 alternative 构建 tsquery

sql - PostgreSQL,带间隔的累积量

mysql - 将目录中的数据同步到数据库的实用方法是什么?

MySQL插入唯一且仅当条件为真

javascript - Knex 迁移失败并显示 - SAVEPOINT can only be used in transaction blocks