-1

下面的代码包含多个 where 条件

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条件的查询给出不同的结果。我如何在不编写子查询的情况下实现。有没有什么办法

4

3 回答 3

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)
于 2013-11-11T11:02:16.343 回答
0

AND 和 OR 运算符

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;
于 2013-11-11T11:05:55.397 回答
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
于 2013-11-11T11:22:59.653 回答