Make MIN(column) return NULL if column contains NULL - sql

SELECT MIN(column) FROM table;
will return minimum from nonnull elements.
I would like to write a query that will treat NULL as if it were the smallest value possible.
I have seen tricks with dates using a special value like here: https://stackoverflow.com/a/32240382/7810882
But what if the column is of type int and there is no special value that I can map NULL to?

You can do this using a case expression:
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table;

Related

DB2 SQL Statement WHERE clause CASE WHEN in multiple conditions

This is what I am trying to do:
SELECT
id, name
FROM
users
WHERE
isActive=true
(AND CASE WHEN {param} != null THEN name={param} ELSE null END)
if the passed {param} is not null then only the AND operator will be added otherwise just isActive=true condition will be used.
You can use something like COALESCE or (in case of DB2) NVL.
SELECT
id, name
FROM
users
WHERE
isActive=true
AND name=COALESCE({param},name)
You didn't say how you pass parameters/variables, so i'll leave it to you. "{param}" could be replaced with a column or constant in this example
try this
SELECT
id, name
FROM
users
WHERE
isActive=true
AND (({param} is not null AND name={param}) OR ({param} is null))

SQL - Return record with a null value only if there is no matching record that is not null

I have a table that has some multiple entries for certain fields but not others. Here's an illustration:
I need to write a query that will return a distinct list of the 'Type' values with the numeric value if a non-null record exists, but with the null value only if there is no matching 'Type' value with a corresponding numeric value. So, for the table above, I need a recordset like this:
I thought this would be easy, but I've been scratching my head over it. Any help appreciated.
You can use aggregation:
select type, max(col2)
from t
group by type;
This guarantees that only one row is returned for each value in the first column and you only get NULL when all the values are NULL.
If you want all non-null values and a representative NULL one of the others do not exist, you can use logic like this:
select type, col2
from t
where col2 is not null
union al
select type, null
from t
group by type
having max(col2) is null;

Select from table with 2 condition and 2 operations

I have a Table BoxTrans
the table Contain Rows (ID,Date,FromBox,ToBox,Value)
I want to make a View like (ID,Date,Box,ValueIn,ValueOut)
select when frombox Give Value to ValueOut
and when tobox Give Value to ValueIN
You can use a CASE statement to check the value of a different column when populating a column. The below query will return your output as long as either ToBox or FromBox is NULL, if they are both not null you may get unexpected results.
SELECT ID,
Date,
COALESCE(ToBox,FromBox) as Box,
CASE WHEN ToBox IS NOT NULL THEN value ELSE NULL as ValueIn,
CASE WHEN FromBox IS NOT NULL THEN value ELSE NULL as ValueOut
FROM BoxTrans

SQL query to get null count from a column

I want to generate a SQL query to get null count in a particular column like
SELECT COUNT (column) AS count
FROM table
WHERE column = null ;
This is returning 0, but I want how many null values are present in that column like
SELECT COUNT (column) AS count
FROM table
WHERE column = 'some value';
which returns the count of the matched records
NULL value is special in that you cannot use = with it; you must use IS NULL instead:
SELECT COUNT (*) AS count FROM table where column IS null ;
This is because NULL in SQL does not evaluate as equal to anything, including other NULL values. Also note the use of * as the argument of COUNT.
You can use a conditional sum()
SELECT sum(case when column is null
then 1
else 0
end) AS count
FROM table
A different query but exact answer check it out
select count(*)-count(column) from table
please vote check this as answer if it helps you
To get exact output you can use below command as well -
SELECT COUNT (*) AS count FROM table where column IS null OR column='';
because some times only '' doesn't counted as NULL.

How to display Items that equal a value in one column and are not null in another

Let's say I want to display all items in a table with following criteria how would i do this?
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 != 'NULL'
I want it to display all values from COLUMN1. How does one go about this process in MS SQL?
NULL values can be compared using IS [NOT] NULL in SQL server. Please check this.
SELECT *
FROM TABLE
WHERE TABLE.COLUMN1 = 'example' AND TABLE.COLUMN2 IS NOT NULL
NULL is an UNKNOWN value , you cannot use any Comparison Operators (= , <> , > , <) with it. you check for nulls like
ColumnName IS NULL or ColumnName IS NOT NULL
If you think about it , it makes sense, to compare two or more values, you need to know the values only then you can compare them, Since SQL Server considers a NULL to be an UNKNOWN value, you cannot really compare an unknown value to anything.