将char var转换为整数以用于连接

时间:2019-05-30 15:17:41

标签: sql postgresql crystal-reports

我正在尝试使用我们的postgres数据库来获取一份报告,以便在Crystal中运行。

我的SQL看起来像

SELECT 
    slcnote.note_date, 
    slcnote.account, 
    customer.name, 
    slcnote.reference,
    slcnote.operator,
    salesman.name,  
    slcnote.system_date,
    slcnote.note_type, 
    slcnote.note_note,
                CAST(salesman.code as integer)
FROM   
   slcnote
    Left JOIN customer ON slcnote.account=customer.account
    left JOIN salesman ON slcnote.operator=salesman.code
 ORDER BY "slcnote"."note_date"

但是,似乎并没有将推销员代码转换为整数。我在Crystal Reports中遇到以下错误消息:

--------------------------- Crystal Reports --------------------------- 
Failed to retrieve data from the database. 
Details: 42883:
ERROR: operator does not exist: integer = character varying; 
Error while executing the query 
[Database Vendor Code: 7 ] 
--------------------------- OK ---------------------------

感谢帮助

克里斯

1 个答案:

答案 0 :(得分:1)

您的问题是,在salesman.code子句中将SELECT强制转换为整数,但在ON子句中未强制转换。因此,查询试图通过将integerVARCHAR进行比较来联接表,从而生成错误消息。

要解决您眼前的问题,您需要在ON子句中执行强制转换:

SELECT * 
FROM slcnote
    LEFT JOIN salesman ON CAST(salesman.code AS INT) = slcnote."operator"

但是,如注释中所述,此查询的性能将很糟糕。如果无法更改表,是否可以创建执行转换的实例化视图?这可能会有所帮助。您还应该引用这些性能问题,看看能够 更改表的个人是否会这样做。