简体   繁体   中英

How to get the data and count null and non-null values in sql of multiple columns specific dates

I need help to get the data and count null and non-null values in sql of multiple columns specific dates in a table. I know how to do it get the all values of null and non-null of specific dates in single query separated as below, but I don't know how to incorporate it with all columns with values of null and non-null counted in one single query.

SELECT 
    RECEIPTKEY,
    RECEIPTDATE,
    CLOSEDDATE,
    VERIFIEDCLOSEDDATE,
    ArrivalDateTime,
    ACTUALDEPARTUREDATE,
    DOOR,
    SUSR1,
    SUSR4,
    SUSR5,
    SHIPFROMCITY,
    CARRIERKEY,
    DriverName,
    TrailerNumber,
    TrailerOwner,
    TEMPERATURE,
    ADDDATE,
    ADDWHO
FROM
    wmwhse1.RECEIPT
WHERE
    ADDDATE BETWEEN '2021-09-01' AND '2022-09-30'
ORDER BY ADDDATE DESC

Now I need to extract values of null and non-null values and count them of specific dates

What's the query to get this result?

SUM() can be used in MYSQL count the null values and not null values of each specific column

Example; to count the null values:

SELECT SUM(ISNULL(COLUMN NAME) as 'COLUMN NAME null count' From SCHEMANAME.TABLE where CONDITIONS...

To get the NOT null values, simply add an ! to the front of ISNULL to reverse its meaning.

Example: SUM(!ISNULL(COLUMN NAME)

In your case, if you want to count the number of null and not null values of every column you listed within the specified date range, it's a bit repetitive, but the following query will get you the results you are looking for:

SELECT 
    SUM(ISNULL(RECEIPTKEY)) as 'RECEIPTKEY null count',
    SUM(!ISNULL(RECEIPTKEY)) as 'RECEIPTKEY NOT null count',
    SUM(ISNULL(RECEIPTDATE)) AS 'RECEIPTDATE null count',
    SUM(! ISNULL(RECEIPTDATE)) AS 'RECEIPTDATE NOT null count',
    SUM(ISNULL(CLOSEDDATE)) AS 'CLOSEDDATE null count',
    SUM(! ISNULL(CLOSEDDATE)) AS 'CLOSEDDATE NOT null count',
    SUM(ISNULL(VERIFIEDCLOSEDDATE)) AS 'VERIFIEDCLOSEDDATE null count',
    SUM(! ISNULL(VERIFIEDCLOSEDDATE)) AS 'VERIFIEDCLOSEDDATE NOT null count',
    SUM(ISNULL(ArrivalDateTime)) AS 'ArrivalDateTime null count',
    SUM(! ISNULL(ArrivalDateTime)) AS 'ArrivalDateTime NOT null count',
    SUM(ISNULL(ACTUALDEPARTUREDATE)) AS 'ACTUALDEPARTUREDATE null count',
    SUM(! ISNULL(ACTUALDEPARTUREDATE)) AS 'ACTUALDEPARTUREDATE NOT null count',
    SUM(ISNULL(DOOR)) AS 'DOOR null count',
    SUM(! ISNULL(DOOR)) AS 'DOOR NOT null count',
    SUM(ISNULL(SUSR1)) AS 'SUSR1 null count',
    SUM(! ISNULL(SUSR1)) AS 'SUSR1 NOT null count',
    SUM(ISNULL(SUSR4)) AS 'SUSR4 null count',
    SUM(! ISNULL(SUSR4)) AS 'SUSR4 NOT null count',
    SUM(ISNULL(SUSR5)) AS 'SUSR5 null count',
    SUM(! ISNULL(SUSR5)) AS 'SUSR5 NOT null count',
    SUM(ISNULL(SHIPFROMCITY)) AS 'SHIPFROMCITY null count',
    SUM(! ISNULL(SHIPFROMCITY)) AS 'SHIPFROMCITY NOT null count',
    SUM(ISNULL(CARRIERKEY)) AS 'CARRIERKEY null count',
    SUM(! ISNULL(CARRIERKEY)) AS 'CARRIERKEY NOT null count',
    SUM(ISNULL(DriverName)) AS 'DriverName null count',
    SUM(! ISNULL(DriverName)) AS 'DriverName NOT null count',
    SUM(ISNULL(TrailerNumber)) AS 'TrailerNumber null count',
    SUM(! ISNULL(TrailerNumber)) AS 'TrailerNumber NOT null count',
    SUM(ISNULL(TrailerOwner)) AS 'TrailerOwner null count',
    SUM(! ISNULL(TrailerOwner)) AS 'TrailerOwner NOT null count',
    SUM(ISNULL(TEMPERATURE)) AS 'TEMPERATURE null count',
    SUM(! ISNULL(TEMPERATURE)) AS 'TEMPERATURE NOT null count',
    SUM(ISNULL(ADDWHO)) AS 'ADDWHO null count',
    SUM(! ISNULL(ADDWHO)) AS 'ADDWHO NOT null count'
FROM
    wmwhse1.RECEIPT
WHERE
    ADDDATE BETWEEN '2021-09-01' AND '2022-09-30'
ORDER BY ADDDATE DESC

Note: I did not include the ADDDATE column in the SELECT clause, since the where clause of this query could never yield a null result for that column. It's also noteworthy that a Primary Key column will never allow null values, so there really isn't any reason to include those in such a query either.

If the ADDDATE null values are also needed, you will want to do that as a separate query because there is no guarantee that the null values occurred during the date range specified. The Where clause of such a query would be this:

WHERE ADDDATE is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM