【问题标题】:No function matches the given name and argument types. You might need to add explicit type casts - Postgresql (IF function)没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换 - Postgresql(IF 函数)
【发布时间】:2021-06-23 09:10:31
【问题描述】:

我尝试运行以下查询:

SELECT account_no, month_id, IF (product_category = 'Services', 'ServicesMarketing', product_category) AS product_category, revenue  
FROM public.revenue_raw_data

我得到的错误:

ERROR:  function if(boolean, unknown, character) does not exist
LINE 1: SELECT account_no, month_id, IF (product_category = 'Service...
                                     ^
HINT:  No function matches the given name and argument types. 
You might need to add explicit type casts.
SQL state: 42883
Character: 30

我的数据:

【问题讨论】:

    标签: sql postgresql case


    【解决方案1】:

    SQL(或 Postgres)中没有 IF() 函数。

    在 Postgres(和标准 SQL)中,您将使用 CASE 表达式

    SELECT account_no, month_id, 
           case 
             when product_category = 'Services'
                  then 'ServicesMarketing'
             else product_category
           end AS product_category, 
           revenue 
    FROM public.revenue_raw_data
    

    (请注意,我只是猜测您认为if() 应该做什么)

    【讨论】:

    • 太棒了!我尝试了 CASE 表达式,它起作用了。谢谢
    猜你喜欢
    • 2020-08-26
    • 2016-01-18
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多