PSQL错误:无法找到从未知到字符变化的转换函数

时间:2018-05-21 05:29:11

标签: postgresql psql postgresql-9.5

所以我有以下PSQL查询返回此错误。问题似乎是coalesce(d.name, e.first_name, g.name) as sourcedest,中的sales行被Postgres视为character varying。因为

而输出UNION错误
    {li> 'sdfsdf' as sourcedest resupply {li> NULL as sourcedest audit

    被视为unknown。有没有办法强制改变角色?

        SELECT * 
        FROM (
        WITH
        sales AS (
            SELECT a.fk_item_id, a.date_time_created, a.quantity, 
            CASE
                WHEN a.fk_sale_id is NULL THEN CONCAT('Meter Reading: ', a.instance_type)
                ELSE a.instance_type
            END as instance_type,
            coalesce(d.name, e.first_name, g.name) as sourcedest,
            CASE
                WHEN a.fk_sale_id is NOT NULL THEN CONCAT(h.first_name, ' ', h.last_name)
                ELSE CONCAT(i.first_name, ' ', i.last_name)
            END as employee
    
            FROM sales_iteminstance a
            LEFT JOIN sales_sale b ON b.id = a.fk_sale_id
            LEFT JOIN sales_meterreading c ON c.id = a.fk_meter_reading_id
    
            LEFT JOIN pr_creditline d ON d.id = b.fk_creditline_id
            LEFT JOIN pr_client e ON e.id = b.fk_client_id
    
            LEFT JOIN pr_gasmeter f ON f.id = c.fk_gas_meter_id
            LEFT JOIN pr_creditline g ON g.id = f.fk_creditline_id
    
            LEFT JOIN hr_employee h ON h.id = b.fk_employee_id
            LEFT JOIN hr_employee i ON i.id = c.fk_employee_id
        ),
        resupply AS (
            SELECT a.fk_item_id, a.date_time_created, a.quantity,
            CASE
             WHEN (b.movement = 'Inbound') THEN 'Resupply In'
             WHEN (b.movement = 'Outbound') THEN 'Resupply Out'
            END AS instance_type,
            'sdfsdf' as sourcedest,
            CONCAT(c.first_name, ' ', c.last_name) as employee
    
            FROM inventory_resupplylogiteminstance a
            INNER JOIN inventory_resupplylog b ON b.id = a.fk_resupply_log_id
            INNER JOIN hr_employee c ON c.id = b.fk_employee_id
        ),
        audit AS (
            SELECT a.fk_item_id, a.date_time_created, a.difference as quantity, 'Audit' as instance_type, NULL as sourcedest, CONCAT(c.first_name, ' ', c.last_name)
            FROM inventory_itemauditinstance a
            INNER JOIN inventory_itemaudit b ON b.id = a.fk_item_audit_id
            INNER JOIN hr_employee c ON c.id = b.fk_employee_id
        )
        SELECT * FROM sales
        UNION
        SELECT * FROM resupply
        UNION
        SELECT * FROM audit
        ) as res
    
        WHERE fk_item_id = {2} AND date_time_created >= ('{0}'::date) AND date_time_created < ('{1}'::date)
        ORDER BY date_time_created DESC
    

    有人可以告诉我这有什么问题吗?并提供有关如何在将来调试此类错误的提示。

    编辑: 想出来了,我只需要将'sdfsdf' as sourcedest更改为cast('sdfsdf' character varying) as sourcedest

0 个答案:

没有答案