oracle sql - 仅在该列中没有空值时选择

时间:2014-09-17 21:32:47

标签: sql oracle notnull

我正在查看包含几个唯一清单项目的清单。我只想选择清单日期字段中没有空值的人的记录(ID,名称等)。

Select distinct sp.id as "ID",
                SP.LAST_NAME as "Last",
                SP.FIRST_NAME as "First",
                SA.TERM_CODE_ENTRY as "Term", 
                SA.APST_CODE as "Status"
FROM SPRITE SP
JOIN SARC CK
    on sp.sprite_pidm = CK.SARC_pidm 
JOIN ADAP SA
    on sp.sprite_pidm = sa.adap_PIDM
WHERE
    Sp.sprite_change_ind is null
and SA.ADAP_TERM_CODE_ENTRY = '201480'
and SA.ADAP_APST_CODE = 'I'
and SA.ADAP_APPL_NO  = CK.SARC_APPL_NO
-- where there are no null records - all records should be not null
and CK.SARC_RECEIVE_DATE is not null

目前,它正在选择至少有一个非空清单日期的人。这意味着它仍在为某些清单项目选择空日期的记录。

如何告诉它选择在哪里 CK.SARC_RECEIVE_DATE =(所有清单项目receive_dates必须是非空值)?

简化示例:

ID    Name    Checklist Items    DateReceived       Other data...
01    Sherry  missing item 1     
01    Sherry  missing item 2     02-02-14
05    Mike    missing item 8     02-03-13
17    Carl    missing item 2     
17    Carl    missing item 3     
28    Luke    missing item 3     04-03-13    
28    Luke    missing item 5     04-03-13
28    Luke    missing item 8     04-03-13

The results should be
05    Mike (other data...)
28    Luke (other data...)

Instead, it is returning
01    Sherry (other data...)
05    Mike   (other data...)
28    Luke   (other data...)

3 个答案:

答案 0 :(得分:0)

你不会这样做。相反,使用分析函数来计算NULL值的数量,并选择不具有任何值的值。这是一个想法:

with t as (
      <your query here>
     )
select *
from (select t.*, sum(case when SARC_RECEIVE_DATE is null then 1 else 0 end) as numNulls
      from t
     ) t
where numNulls = 0;

答案 1 :(得分:0)

WITH MYVIEW AS
(
    Select   sp.id as ID,
             SP.LAST_NAME ,
             SP.FIRST_NAME,
             SA.TERM_CODE_ENTRY, 
             SA.APST_CODE,
             CK.SARC_RECEIVE_DATE As RECEIVED_DATE
    FROM SPRITE SP
    JOIN SARC CK
        on sp.sprite_pidm = CK.SARC_pidm 
    JOIN ADAP SA
        on sp.sprite_pidm = sa.adap_PIDM
    WHERE
        Sp.sprite_change_ind is null
    and SA.ADAP_TERM_CODE_ENTRY = '201480'
    and SA.ADAP_APST_CODE = 'I'
    and SA.ADAP_APPL_NO  = CK.SARC_APPL_NO
)
SELECT ID as "ID",
       MAX(LAST_NAME) as "Last",
       MAX(FIRST_NAME) as "First",
       MAX(TERM_CODE_ENTRY) as "Term",
       MAX(APST_CODE) as "Status"
 FROM MY_VIEW
GROUP BY id 
HAVING SUM(NVL2(RECEIVED_DATE,0,1)) = 0;

答案 2 :(得分:0)

我根据您提供的示例回答。

您也可以使用NOT EXISTS,因为您没有从表SARC中进行选择。

/*
WITH sprite AS (SELECT 1 sprite_id, 'Sherry' Name FROM dual
                UNION ALL SELECT 5, 'Mike' FROM dual
                UNION ALL SELECT 17, 'Carl' FROM dual
                UNION ALL SELECT 28, 'Luke' FROM dual),
sarc AS (
    SELECT 1 sprite_id, 'missing item' checklist, 1 items, null AS dateReceived FROM dual
    UNION ALL SELECT 1, 'missing item', 2, to_date('02-02-2014', 'dd-mm-yyyy') FROM dual
    UNION ALL SELECT 5, 'missing item', 8, to_date('02-03-2014', 'dd-mm-yyyy') FROM dual
    UNION ALL SELECT 17, 'missing item', 2, null FROM dual
    UNION ALL SELECT 17, 'missing item', 3, null FROM dual
    UNION ALL SELECT 28, 'missing item', 3, to_date('04-03-2014', 'dd-mm-yyyy') FROM dual
    UNION ALL SELECT 28, 'missing item', 5, to_date('04-03-2014', 'dd-mm-yyyy') FROM dual
    UNION ALL SELECT 28, 'missing item', 8, to_date('04-03-2014', 'dd-mm-yyyy') FROM dual)
-- */
SELECT distinct sp.sprite_id, sp.name
  FROM sprite sp
 WHERE NOT EXISTS (SELECT 1
                     FROM sarc sa
                    WHERE sa.sprite_id = sp.sprite_id
                          AND dateReceived IS NULL);

结果完全符合您的要求。

我认为对您的查询进行以下修改应该对您有用。我不确定确切的结果,因为我不知道您的样本数据,但这应该稍作修改:

SELECT DISTINCT sp.id AS "ID",
                SP.LAST_NAME AS "Last",
                SP.FIRST_NAME AS "First",
                SA.TERM_CODE_ENTRY AS "Term",
                SA.APST_CODE AS "Status"
  FROM SPRITE SP
       JOIN ADAP SA ON sp.sprite_pidm = sa.adap_PIDM
 WHERE     Sp.sprite_change_ind IS NULL
       AND SA.ADAP_TERM_CODE_ENTRY = '201480'
       AND SA.ADAP_APST_CODE = 'I'
       AND NOT EXISTS (SELECT 1
                         FROM sarc ck
                        WHERE     ck.sarc_appl_no = sa.adap_appl_no
                              AND ck.sarc_receive_date IS NULL);