我是 if-then SQL 语句的新手。我只知道基础知识(选择、更新、插入连接等),因此如果您能帮助我了解这种情况下的语法,将会很有帮助。
我有一个包含客户活动的表,假设我是一名牙医,我存储了一个特定的活动,当使用我的软件检查客户应该支付的费用时,我使用这个查询:
SELECT ACTIVITY.ID,
ACTIVITY.DATES,
ACTIVITY.INSURANCE_ID
ACTIVITY.AMOUNT,
ACTIVITY.INSURANCE_AMOUNT,
ACTIVITY.AMOUNT * ((100 - ACTIVITY.INSURANCE_AMOUNT) / 100) AS AMOUNT_TO_PAY,
ACTIVITY.TIME,
ACTIVITY.MONEY_RECEIVED,
ACTIVITY.ROWID
FROM ACTIVITY
LEFT JOIN INSURANCE ON INSURANCE.ID = ACTIVITY.INSURANCE_ID
WHERE ACTIVITY.ID = :patient_id AND ACTIVITY.MONEY_RECEIVED IS NULL
此查询选择我需要的数据加上客户应支付的金额,计算保险金额的百分比折扣并返回作为货币支付的总金额 (AMOUNT_TO_PAY)。
这很好用,但问题是,即使保险已过期,这种计算也会发生。我想用 if-then 或 ORACLE JDEVELOPER 中的任何其他方法创建一个 SQL 语句,首先将检查我的表 CUSTOMER_INSURANCE.TO_DATE 是否仍然处于活动状态;如果是,请进行计算;如果 DATE 已过期,则不包括保险百分比。
这是我正在尝试执行的查询,但我做错了:
IF ((SELECT count(*) FROM CUSTOMER_INSURANCE.TO_DATE TO_DATE
WHERE TO_DATE >= sysdate AND customer_id = :patient_id) > 0)
SELECT ACTIVITY.ID,
ACTIVITY.DATES,
ACTIVITY.INSURANCE_ID
ACTIVITY.AMOUNT,
ACTIVITY.INSURANCE_AMOUNT,
ACTIVITY.AMOUNT * ((100 - ACTIVITY.INSURANCE_AMOUNT) / 100) AS AMOUNT_TO_PAY,
ACTIVITY.TIME,
ACTIVITY.MONEY_RECEIVED,
ACTIVITY.ROWID
FROM ACTIVITY
LEFT JOIN INSURANCE ON INSURANCE.ID = ACTIVITY.INSURANCE_ID
WHERE ACTIVITY.ID = :patient_id AND ACTIVITY.MONEY_RECEIVED IS NULL
ELSE IF ((SELECT count(*) FROM CUSTOMER_INSURANCE.TO_DATE TO_DATE
WHERE TO_DATE >= sysdate AND customer_id = :patient_id) = 0)
SELECT ACTIVITY.ID,
ACTIVITY.DATES,
ACTIVITY.INSURANCE_ID
ACTIVITY.AMOUNT,
ACTIVITY.INSURANCE_AMOUNT,
ACTIVITY.AMOUNT AS AMOUNT_TO_PAY,
ACTIVITY.TIME,
ACTIVITY.MONEY_RECEIVED,
ACTIVITY.ROWID
FROM ACTIVITY
LEFT JOIN INSURANCE ON INSURANCE.ID = ACTIVITY.INSURANCE_ID
WHERE ACTIVITY.ID = :patient_id AND ACTIVITY.MONEY_RECEIVED IS NULL
谁能帮我完成这个查询?