以下代码包含多个条件
select SUM(COMMONSPEC.DISBURSE_AMT)
from spec_gt1 COMMONSPEC
where COMMONSPEC.instance_code_1 = 3
OR COMMONSPEC.instance_code_2 = 3
OR COMMONSPEC.instance_code_3 = 3
OR COMMONSPEC.instance_code_4 = 3
OR COMMONSPEC.instance_code_5 = 3;
其中一个条件赋予null,因此具有 AND 条件的查询将为null,而 OR 条件则给出不同的结果。如何在不编写子查询的情况下实现。有没有办法
答案 0 :(得分:0)
您可以使用And
is Not null
select SUM(COMMONSPEC.DISBURSE_AMT)
from spec_gt1 COMMONSPEC
where (COMMONSPEC.instance_code_1 =
3 and COMMONSPEC.instance_code_1 is not null)
OR (COMMONSPEC.instance_code_2 =
3 and COMMONSPEC.instance_code_2 is not null)
OR (COMMONSPEC.instance_code_3 =
3 and COMMONSPEC.instance_code_3 is not null)
OR (COMMONSPEC.instance_code_4 =
3 and COMMONSPEC.instance_code_4 is not null)
OR (COMMONSPEC.instance_code_5 =
3 and COMMONSPEC.instance_code_5 is not null)
答案 1 :(得分:0)
First of all you have to know about what is AND and OR?
When AND is placed the all columns values are match to 3.
示例:强>
where COMMONSPEC.instance_code_1 = 3
AND COMMONSPEC.instance_code_2 = 3
AND COMMONSPEC.instance_code_3 = 3
AND COMMONSPEC.instance_code_4 = 3
AND COMMONSPEC.instance_code_5 = 3;
和
When OR is placed if any one column value is match to 3
then it will show the record.
示例:强>
where COMMONSPEC.instance_code_1 = 3
OR COMMONSPEC.instance_code_2 = 3
OR COMMONSPEC.instance_code_3 = 3
OR COMMONSPEC.instance_code_4 = 3
OR COMMONSPEC.instance_code_5 = 3;
答案 2 :(得分:0)
您可以使用COALESCE
SELECT
SUM ( DISBURSE_AMT )
FROM
SPEC_GT1
WHERE
COALESCE ( INSTANCE_CODE_1,
INSTANCE_CODE_2,
INSTANCE_CODE_3,
INSTANCE_CODE_4,
INSTANCE_CODE_5 ) = 3;
您可以尝试以下示例:
WITH SPEC_GT1
AS (SELECT
1 AS DISBURSE_AMT,
3 AS INSTANCE_CODE_1,
3 AS INSTANCE_CODE_2,
3 AS INSTANCE_CODE_3,
NULL AS INSTANCE_CODE_4,
3 AS INSTANCE_CODE_5
FROM
DUAL
UNION ALL
SELECT
2 AS DISBURSE_AMT,
3 AS INSTANCE_CODE_1,
3 AS INSTANCE_CODE_2,
3 AS INSTANCE_CODE_3,
3 AS INSTANCE_CODE_4,
3 AS INSTANCE_CODE_5
FROM
DUAL
UNION ALL
SELECT
3 AS DISBURSE_AMT,
3 AS INSTANCE_CODE_1,
NULL AS INSTANCE_CODE_2,
3 AS INSTANCE_CODE_3,
3 AS INSTANCE_CODE_4,
3 AS INSTANCE_CODE_5
FROM
DUAL
UNION ALL
SELECT
4 AS DISBURSE_AMT,
3 AS INSTANCE_CODE_1,
3 AS INSTANCE_CODE_2,
2 AS INSTANCE_CODE_3,
3 AS INSTANCE_CODE_4,
3 AS INSTANCE_CODE_5
FROM
DUAL)
SELECT
SUM ( DISBURSE_AMT )
FROM
SPEC_GT1
WHERE
COALESCE ( INSTANCE_CODE_1,
INSTANCE_CODE_2,
INSTANCE_CODE_3,
INSTANCE_CODE_4,
INSTANCE_CODE_5 ) = 3