如何使用分析函数忽略case表达式中的空值?

时间:2016-06-01 09:36:42

标签: sql oracle

我有以下情况:

这是我的SQL:

SELECT
NAME,
CASE WHEN ECHUE LIKE 'echue' 
  THEN sum(RESTE) over (partition by NAME ,ECHUE) 
END 
    as TOT_RESTE_ECHUE, 
sum(RESTE) over (partition by NAME) as TOT_RESTE_CLIENT,
FROM TABLE

我在TABLE中有什么:

 Name      | ECHUE     |RESTE
-----------|-----------|---------
John       | ECHUE     | 20
John       | ECHUE     | 20
John       | null      | 20
John       | ECHUE     | 20

我得到了什么:

 Name      | ECHUE     |RESTE    |TOT_RESTE_ECHUE | TOT_RESTE_CLIENT
-----------|-----------|---------|----------------|------------------
John       | ECHUE     | 20      |60              |80
John       | ECHUE     | 20      |60              |80
John       | null      | 20      |null            |80
John       | ECHUE     | 20      |60              |80

我想要的是什么:

 Name      | ECHUE     |RESTE    |TOT_RESTE_ECHUE | TOT_RESTE_CLIENT
-----------|-----------|---------|----------------|-------------------
John       | ECHUE     | 20      |60              |80
John       | ECHUE     | 20      |60              |80
John       | null      | 20      |60              |80
John       | ECHUE     | 20      |60              |80

我正在使用ORACLE数据库版本12。 我不能使用Where ECHUE IS NOT NULL,因为我需要将TOT_RESTE_CLIENT计算为correclty。

3 个答案:

答案 0 :(得分:2)

Oracle安装程序

CREATE TABLE table_name ( name, echue, reste ) AS
SELECT 'John', 'ECHUE', 20 FROM DUAL UNION ALL
SELECT 'John', 'ECHUE', 20 FROM DUAL UNION ALL
SELECT 'John', NULL,    20 FROM DUAL UNION ALL
SELECT 'John', 'ECHUE', 20 FROM DUAL;

<强>查询

SELECT t.*
       SUM( CASE WHEN echue IS NOT NULL THEN reste END )
         OVER ( PARTITION BY name ) AS tot_reste_echue
FROM   table_name t;

<强>输出

NAME ECHUE RESTE TOT_RESTE_ECHUE
---- ----- ----- ---------------
John ECHUE    20              60
John ECHUE    20              60
John          20              60
John ECHUE    20              60

如果您还需要PARTITION BY ECHUE列,并将NULL'ECHUE'值包括在一起,那么您可以这样做:

SELECT t.*
       SUM( CASE WHEN echue IS NOT NULL THEN reste END )
         OVER ( PARTITION BY name, COALESCE( echue, 'ECHUE' ) ) AS tot_reste_echue
FROM   table_name t;

答案 1 :(得分:0)

获取所需输出的查询如下:

SELECT 
   name,
   echue,
   reste,
   (SELECT SUM(reste) FROM test t2 WHERE t2.name = t1.name) AS tot_reste_echue
FROM test t1

答案 2 :(得分:0)

以下是使用null ECHUE行获取结果的一种方法:

with sample_data as (select 'John' name, 'ECHUE' echue, 20 reste from dual union all
                     select 'John' name, null echue, 20 reste from dual union all
                     select 'John' name, 'ECHUE' echue, 20 reste from dual union all
                     select 'John' name, 'ECHUE' echue, 20 reste from dual)
-- end of mimicking a table called sample_data with your data in it. See SQL below:
select name,
       echue,
       reste,
       case when echue = 'ECHUE' or echue is null then
                 sum(case when echue is null then 0 else reste end) over (partition by name)
       end tot_reste_echue
from   sample_data;

NAME ECHUE      RESTE TOT_RESTE_ECHUE
---- ----- ---------- ---------------
John ECHUE         20              60
John ECHUE         20              60
John ECHUE         20              60
John               20              60