无法滤除NULL包含列

时间:2018-08-01 11:09:47

标签: sql sql-server

我加入了5个表,得到了最终结果,这是下面给出的查询。

SELECT *
FROM (SELECT O.CUSTADDRESSID,
             O.ACCOUNTNO,
             O.ADDRESSTYPE,
             O.ADDRESSLINE1,
             O.ADDRESSLINE2,
             O.ADDRESSLINE3,
             O.CITY,
             O.STATE,
             O.COUNTRY,
             O.ZIP1,
             O.ISACTIVE AS ISACTIVE_ADDRESS,
             O.ISCOMMUNICATION AS ISCOMMUNICATION_ADDRESS,
             D.ORGANISATIONNAME,
             D.DATEOFINCORPORATION,
             D.PANCARDNUMBER,
             D.ORGANIZATIONTYPEID,
             P.CUSTMAILID,
             P.EMAILTYPE,
             P.EMAILADDRESS,
             P.ISACTIVE AS ISACTIVE_MAILID,
             P.ISCOMMUNICATION AS ISCOMMUNICATION_MAILID,
             Q.LOGINID,
             Q.USERNAME,
             Q.PASSWORD,
             Q.LAST_LOGINDATE,
             Q.LAST_PWD_MODIFIEDDATE,
             Q.CURRENT_PWD_EXPIRYDATE,
             Q.PWD_ATTEMPTS_COUNT,
             Q.PINNUMBER,
             Q.ISLOCKED,
             Q.THEMES,
             Q.LANGUAGES,
             Q.STATUSID,
             Q.USERTYPEID,
             Q.ROLENAME,
             Q.SQ_ATTEMPTCOUNT,
             Q.SQ_LOCKOUTTIME,
             R.CUSTPHONEID,
             R.PHONETYPE,
             R.PHONENUMBER,
             R.EXTENTION,
             R.ISACTIVE AS ISACTIVE_PHONES,
             R.ISCOMMUNICATION AS ISCOMMUNICATION_PHONES
      FROM ISSUER.TOLLPLUS.TP_CUSTOMER_ADDRESSES O
           FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_BUSINESS D ON O.ACCOUNTNO = D.ACCOUNTNO
           FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_EMAILS P ON D.ACCOUNTNO = P.ACCOUNTNO
           FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_LOGINS Q ON P.ACCOUNTNO = Q.ACCOUNTNO
           FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_PHONES R ON Q.ACCOUNTNO = R.ACCOUNTNO) A
GROUP BY CUSTADDRESSID,
         ACCOUNTNO,
         ADDRESSTYPE,
         ADDRESSLINE1,
         ADDRESSLINE2,
         ADDRESSLINE3,
         CITY,
         STATE,
         COUNTRY,
         ZIP1,
         ISACTIVE_ADDRESS,
         ISCOMMUNICATION_ADDRESS,
         ORGANISATIONNAME,
         DATEOFINCORPORATION,
         PANCARDNUMBER,
         ORGANIZATIONTYPEID,
         CUSTMAILID,
         EMAILTYPE,
         EMAILADDRESS,
         ISACTIVE_MAILID,
         ISCOMMUNICATION_MAILID,
         LOGINID,
         USERNAME,
         PASSWORD,
         LAST_LOGINDATE,
         LAST_PWD_MODIFIEDDATE,
         CURRENT_PWD_EXPIRYDATE,
         PWD_ATTEMPTS_COUNT,
         PINNUMBER,
         ISLOCKED,
         THEMES,
         LANGUAGES,
         STATUSID,
         USERTYPEID,
         ROLENAME,
         SQ_ATTEMPTCOUNT,
         SQ_LOCKOUTTIME,
         CUSTPHONEID,
         PHONETYPE,
         PHONENUMBER,
         EXTENTION,
         ISACTIVE_PHONES,
         ISCOMMUNICATION_PHONES
ORDER BY ACCOUNTNO;

例如,给出4条结果行:

    CUSTADDRESSID   ACCOUNTNO   ADDRESSTYPE ADDRESSLINE1    ADDRESSLINE2    ADDRESSLINE3    CITY    STATE   COUNTRY ZIP1    ISACTIVE_ADDRESS    ISCOMMUNICATION_ADDRESS ORGANISATIONNAME    DATEOFINCORPORATION PANCARDNUMBER   ORGANIZATIONTYPEID  CUSTMAILID  EMAILTYPE   EMAILADDRESS    ISACTIVE_MAILID ISCOMMUNICATION_MAILID  LOGINID USERNAME    PASSWORD    LAST_LOGINDATE  LAST_PWD_MODIFIEDDATE   CURRENT_PWD_EXPIRYDATE  PWD_ATTEMPTS_COUNT  PINNUMBER   ISLOCKED    THEMES  LANGUAGES   STATUSID    USERTYPEID  ROLENAME    SQ_ATTEMPTCOUNT SQ_LOCKOUTTIME  CUSTPHONEID PHONETYPE   PHONENUMBER EXTENTION   ISACTIVE_PHONES ISCOMMUNICATION_PHONES
    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    VISHWAS ROADLINES BELGAUM   2009-04-01 00:00:00.000 AWBPK6828R  2103    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
CUSTADDRESSID   ACCOUNTNO   ADDRESSTYPE ADDRESSLINE1    ADDRESSLINE2    ADDRESSLINE3    CITY    STATE   COUNTRY ZIP1    ISACTIVE_ADDRESS    ISCOMMUNICATION_ADDRESS ORGANISATIONNAME    DATEOFINCORPORATION PANCARDNUMBER   ORGANIZATIONTYPEID  CUSTMAILID  EMAILTYPE   EMAILADDRESS    ISACTIVE_MAILID ISCOMMUNICATION_MAILID  LOGINID USERNAME    PASSWORD    LAST_LOGINDATE  LAST_PWD_MODIFIEDDATE   CURRENT_PWD_EXPIRYDATE  PWD_ATTEMPTS_COUNT  PINNUMBER   ISLOCKED    THEMES  LANGUAGES   STATUSID    USERTYPEID  ROLENAME    SQ_ATTEMPTCOUNT SQ_LOCKOUTTIME  CUSTPHONEID PHONETYPE   PHONENUMBER EXTENTION   ISACTIVE_PHONES ISCOMMUNICATION_PHONES
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    WHITEINK CONSULTANCY PRIVATE LIMITED    2013-08-27 00:00:00.000 AABCW4232E  3041    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
CUSTADDRESSID   ACCOUNTNO   ADDRESSTYPE ADDRESSLINE1    ADDRESSLINE2    ADDRESSLINE3    CITY    STATE   COUNTRY ZIP1    ISACTIVE_ADDRESS    ISCOMMUNICATION_ADDRESS ORGANISATIONNAME    DATEOFINCORPORATION PANCARDNUMBER   ORGANIZATIONTYPEID  CUSTMAILID  EMAILTYPE   EMAILADDRESS    ISACTIVE_MAILID ISCOMMUNICATION_MAILID  LOGINID USERNAME    PASSWORD    LAST_LOGINDATE  LAST_PWD_MODIFIEDDATE   CURRENT_PWD_EXPIRYDATE  PWD_ATTEMPTS_COUNT  PINNUMBER   ISLOCKED    THEMES  LANGUAGES   STATUSID    USERTYPEID  ROLENAME    SQ_ATTEMPTCOUNT SQ_LOCKOUTTIME  CUSTPHONEID PHONETYPE   PHONENUMBER EXTENTION   ISACTIVE_PHONES ISCOMMUNICATION_PHONES
1   10000000    Business    Madhapur    Mega Hils   NULL    Hyderbad    AP  IND 789564  1   1   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
CUSTADDRESSID   ACCOUNTNO   ADDRESSTYPE ADDRESSLINE1    ADDRESSLINE2    ADDRESSLINE3    CITY    STATE   COUNTRY ZIP1    ISACTIVE_ADDRESS    ISCOMMUNICATION_ADDRESS ORGANISATIONNAME    DATEOFINCORPORATION PANCARDNUMBER   ORGANIZATIONTYPEID  CUSTMAILID  EMAILTYPE   EMAILADDRESS    ISACTIVE_MAILID ISCOMMUNICATION_MAILID  LOGINID USERNAME    PASSWORD    LAST_LOGINDATE  LAST_PWD_MODIFIEDDATE   CURRENT_PWD_EXPIRYDATE  PWD_ATTEMPTS_COUNT  PINNUMBER   ISLOCKED    THEMES  LANGUAGES   STATUSID    USERTYPEID  ROLENAME    SQ_ATTEMPTCOUNT SQ_LOCKOUTTIME  CUSTPHONEID PHONETYPE   PHONENUMBER EXTENTION   ISACTIVE_PHONES ISCOMMUNICATION_PHONES
21  10000789    Business    IBTTA Interoperability          Plano   AP  IND 533126  1   1   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

但是ACCOUNTNO包含NULL。 我想过滤掉ACCOUNTNO中包含的NULL值,所以我给了WHERE ACCOUNTNO != NULL,但结果是我没有值,空行

CUSTADDRESSID   ACCOUNTNO   ADDRESSTYPE ADDRESSLINE1    ADDRESSLINE2    ADDRESSLINE3    CITY    STATE   COUNTRY ZIP1    ISACTIVE_ADDRESS    ISCOMMUNICATION_ADDRESS ORGANISATIONNAME    DATEOFINCORPORATION PANCARDNUMBER   ORGANIZATIONTYPEID  CUSTMAILID  EMAILTYPE   EMAILADDRESS    ISACTIVE_MAILID ISCOMMUNICATION_MAILID  LOGINID USERNAME    PASSWORD    LAST_LOGINDATE  LAST_PWD_MODIFIEDDATE   CURRENT_PWD_EXPIRYDATE  PWD_ATTEMPTS_COUNT  PINNUMBER   ISLOCKED    THEMES  LANGUAGES   STATUSID    USERTYPEID  ROLENAME    SQ_ATTEMPTCOUNT SQ_LOCKOUTTIME  CUSTPHONEID PHONETYPE   PHONENUMBER EXTENTION   ISACTIVE_PHONES ISCOMMUNICATION_PHONES

需要帮助。谢谢。

2 个答案:

答案 0 :(得分:4)

您可以使用IS检查空值

WHERE ACCOUNTNO IS NOT NULL

答案 1 :(得分:0)

这不能回答OP的问题,但是,这里不需要使用子查询,也不需要使用巨大的GROUP BY子句。只需使用DISTINCTUsing DISTINCT with SELECT):

SELECT DISTINCT
       O.CUSTADDRESSID,
       O.ACCOUNTNO,
       O.ADDRESSTYPE,
       O.ADDRESSLINE1,
       O.ADDRESSLINE2,
       O.ADDRESSLINE3,
       O.CITY,
       O.STATE,
       O.COUNTRY,
       O.ZIP1,
       O.ISACTIVE AS ISACTIVE_ADDRESS,
       O.ISCOMMUNICATION AS ISCOMMUNICATION_ADDRESS,
       D.ORGANISATIONNAME,
       D.DATEOFINCORPORATION,
       D.PANCARDNUMBER,
       D.ORGANIZATIONTYPEID,
       P.CUSTMAILID,
       P.EMAILTYPE,
       P.EMAILADDRESS,
       P.ISACTIVE AS ISACTIVE_MAILID,
       P.ISCOMMUNICATION AS ISCOMMUNICATION_MAILID,
       Q.LOGINID,
       Q.USERNAME,
       Q.PASSWORD,
       Q.LAST_LOGINDATE,
       Q.LAST_PWD_MODIFIEDDATE,
       Q.CURRENT_PWD_EXPIRYDATE,
       Q.PWD_ATTEMPTS_COUNT,
       Q.PINNUMBER,
       Q.ISLOCKED,
       Q.THEMES,
       Q.LANGUAGES,
       Q.STATUSID,
       Q.USERTYPEID,
       Q.ROLENAME,
       Q.SQ_ATTEMPTCOUNT,
       Q.SQ_LOCKOUTTIME,
       R.CUSTPHONEID,
       R.PHONETYPE,
       R.PHONENUMBER,
       R.EXTENTION,
       R.ISACTIVE AS ISACTIVE_PHONES,
       R.ISCOMMUNICATION AS ISCOMMUNICATION_PHONES
FROM ISSUER.TOLLPLUS.TP_CUSTOMER_ADDRESSES O
     FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_BUSINESS D ON O.ACCOUNTNO = D.ACCOUNTNO
     FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_EMAILS P ON D.ACCOUNTNO = P.ACCOUNTNO
     FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_LOGINS Q ON P.ACCOUNTNO = Q.ACCOUNTNO
     FULL OUTER JOIN ISSUER.TOLLPLUS.TP_CUSTOMER_PHONES R ON Q.ACCOUNTNO = R.ACCOUNTNO
ORDER BY O.ACCOUNTNO;

此外,您是否由于使用FULL OUTER JOIN而遇到问题?我想知道如果您使用NULL,您的INNER JOIN问题行是否会消失??