PostgreSQL中的嵌套条件条件,语法错误?

问题描述 投票:0回答:2

我使用PostgreSQL,我想结合这两种情况,但是当我取消注释代码时,会出现语法错误。这不是一个困难的指示。我想根据条件除以或乘以获得的数字。如何输入以便编译代码?

SELECT
SUM(

CASE
    WHEN transactions.recipient_account_bill_id = recipientBill.id AND recipientBill.user_id = 2
    THEN 1
    ELSE -1
END * transactions.amount_money

/* CASE
    WHEN senderBillCurrency.id = recipientBillCurrency.id
    THEN NULL
    ELSE
        CASE
            WHEN recipientBillCurrency.base = true
            THEN /
            ELSE *
        END senderBillCurrency.current_exchange_rate
END */

) as TOTAL

FROM transactions

LEFT JOIN bills AS senderBill ON senderBill.id = transactions.sender_account_bill_id
LEFT JOIN bills AS recipientBill ON recipientBill.id = transactions.recipient_account_bill_id
LEFT JOIN currency as senderBillCurrency ON senderBillCurrency.id = senderBill.currency_id
LEFT JOIN currency as recipientBillCurrency ON recipientBillCurrency.id = recipientBill.currency_id

WHERE 2 IN (senderBill.id, recipientBill.id)
sql postgresql
2个回答
0
投票

这是因为您试图返回运算符/*而不是character

CASE
    WHEN senderBillCurrency.id = recipientBillCurrency.id THEN NULL
    ELSE
        CASE
            WHEN recipientBillCurrency.base = true
            THEN '/'
            ELSE '*'
        END || senderBillCurrency.current_exchange_rate
END

0
投票

您无法创建这样的动态SQL表达式。 CASE表达式无法返回运算符,就好像SQL是某种宏语言一样。您只能返回一个表达式。

您已经通过乘法使用1-1使用以下方法。为什么不同时在N1/N中使用它:

<some expression> * CASE
    WHEN senderBillCurrency.id = recipientBillCurrency.id
    THEN NULL
    ELSE CASE
        WHEN recipientBillCurrency.base
        THEN 1 / senderBillCurrency.current_exchange_rate
        ELSE senderBillCurrency.current_exchange_rate
    END 
END
© www.soinside.com 2019 - 2024. All rights reserved.