Group by is null or not? - sql

I want to count how many records in Firebird database have timestamp field null or not null. How to do it?
Something like this:
select RECORD_TYPE, count(*), (MY_TIMESTAMP IS NULL) from MY_TABLE
group by RECORD_TYPE, (MY_TIMESTAMP IS NULL)
I'm using Firebird 2.5

You can output a single record with two columns,
SELECT COUNT(MY_TIMESTAMP) AS TOTAL_NOTNULL,
COUNT(CASE WHEN MY_TIMESTAMP IS NULL THEN 1 END) TOTAL_NULL
FROM MY_TABLE

Related

Counting the number of NULLs in a SQL Server Column

I have two queries like so:
SELECT MyId, MyColumn FROM MyTable WHERE MyColumn IS NULL;
SELECT count(MyColumn) as MyCount FROM MyTable WHERE MyColumn IS NULL;
The results I get are:
MyId MyColumn
10 NULL
Why is the count 0 always in the second query?
The COUNT() function ignores NULL values, and so the count in your second query will always be zero. Either count something else:
SELECT COUNT(*) AS MyCount
FROM MyTable
WHERE MyColumn IS NULL;
Or else count over the entire table using a CASE expression to explicitly count NULL values:
SELECT COUNT(CASE WHEN MyColumn IS NULL THEN 1 END) AS MyCount
FROM MyTable;
Count doesn't count null.
You need to do something like this, transform null to 1 then sum them:
SELECT SUM(CASE WHEN MyColumn IS NULL THEN 1 ELSE 0 END) AS count_nulls
FROM MyTable;
You can simply use count(1) rather than column name in the count function as it ignores null value which counting.
SELECT COUNT(1) AS MyCount
FROM MyTable
WHERE MyColumn IS NULL;
As noted, COUNT(SomeValue) just counts the number of non-nulls, so you actually needed COUNT(*).
But another way is to subtract the non-nulls from the total
SELECT COUNT(*) - COUNT(MyColumn) AS MyCount
FROM MyTable;
A WHERE is probably faster though, especially if you have an index on that column.

Different WHERE clause depending on subquery result

I would like to SELECT WHERE column IS NULL or =value depending on result of subquery.
Here is an example incorrect solution that demonstrates the problem:
SELECT *
FROM table
WHERE column=(
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table
)
When the subquery returns NULL the other query will return nothing because column=NULL is never true. How do I fix this?
(Subquery source: https://stackoverflow.com/a/51341498/7810882)
From your question. just add OR column IS NULL in where clause.
You will get the subquery condition or column IS NULL data.
SELECT *
FROM table
WHERE column= (
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table
) OR column IS NULL
If you are only looking for one row, I would suggest:
select t.*
from table t
order by column nulls first
fetch first 1 row only;

Redshift: Analysis of all columns with null values in all the tables in a database

Need to find the counts of the null and not null values in all columns in all the tables in my database. With below query I can get the info for a single table.
SELECT
'A' as col_name,
COUNT(*) - COUNT(A) as nul_val,
COUNT(A) as nnul_val
FROM table1
UNION
SELECT
'B' as col_name,
COUNT(*) - COUNT(B) as nul_val,
COUNT(B) as nnul_val
FROM table1
I can query information_schema to get list of column names and tables names.
SELECT column_name, table_name from information_schema.columns;
How do I pass the column_name and table_name values from here to my main query? My database is Redshift and it has no provision for variables. Would most likely need to use python UDFs but I'm not sure how to write them for my case.
Python UDFs won't help you - they can only return a single value.
You will need to write a program external to Redshift to collect a list of tables and their columns, then run a query against each table and the table's specific columns.
By the way, there might be a slightly easier way to count the nulls, like this:
SELECT
SUM(CASE WHEN column1 IS NULL THEN 1 END) as column1_null_count,
SUM(CASE WHEN column1 IS NOT NULL THEN 1 END) as column1_not_null_count,
SUM(CASE WHEN column2 IS NULL THEN 1 END) as column2_null_count,
SUM(CASE WHEN column2 IS NOT NULL THEN 1 END) as column2_not_null_count
FROM table

sql query where null results get placed first

select *
from tableA
order by cast(columnA as int), column B.
This is my current query script. There is a scenario where there column A is null. And result rows where column A is null are pushed to the end.
Is there a way such that if columnA is null, put the rows before other rows where columnA is not null?
thanks.
Something along these lines should work if your dbms supports standard SQL.
select (case when columnA is null then 0 else 1 end) as sort_order, *
from tableA
order by sort_order, columnA, columnB;
Try like below... it will help you....
SELECT * FROM tableA ORDER BY (CASE WHEN columnA IS NULL THEN 1 ELSE 0 END) DESC,
CAST(columnA as int), column B
It display the NULL results before NOT NULL Results
I case of Oracle you can use order by columnA NULLS FIRST, columnB

SQL - Getting Most Recent Date From Multiple Columns

Assume a rowset containing the following
EntryID Name DateModified DateDeleted
-----------------------------------------------
1 Name1 1/2/2003 NULL
2 Name1 1/3/2005 1/5/2008
3 Name1 1/3/2006 NULL
4 Name1 NULL NULL
5 Name1 3/5/2008 NULL
Clarification:
I need a single value - the largest non-null date from BOTH columns. So the largest of all ten cells in this case.
SELECT MAX(CASE WHEN (DateDeleted IS NULL OR DateModified > DateDeleted)
THEN DateModified ELSE DateDeleted END) AS MaxDate
FROM Table
For MySQL, Postgres or Oracle, use the GREATEST function:
SELECT GREATEST(ISNULL(t.datemodified, '1900-01-01 00:00:00'),
ISNULL(t.datedeleted, '1900-01-01 00:00:00'))
FROM TABLE t
Both Oracle and MySQL will return NULL if a NULL is provided. The example uses MySQL null handling - update accordingly for the appropriate database.
A database agnostic alternative is:
SELECT z.entryid,
MAX(z.dt)
FROM (SELECT x.entryid,
x.datemodified AS dt
FROM TABLE x
UNION ALL
SELECT y.entryid
y.datedeleted AS dt
FROM TABLE y) z
GROUP BY z.entryid
As a general solution, you could try something like this:
select max(date_col)
from(
select max(date_col1) AS date_col from some_table
union
select max(date_col2) AS date_col from some_table
union
select max(date_col3) AS date_col from some_table
...
)
There might be easier ways, depending on what database you're using.
How about;
SELECT MAX(MX) FROM (
SELECT MAX(DateModified) AS MX FROM Tbl
UNION
SELECT MAX(DateDeleted) FROM Tbl
) T
The answer depends on what you really want. If you simply want the most recent of the two date values then you can do:
Select Max(DateModified), Max(DateDeleted)
From Table
If you are asking for the largest value from either column, then you can simply do:
Select Case
When Max(DateModified) > Max(DateDeleted) Then Max(DateModified)
Else Max(DateDeleted)
End As MaxOfEitherValue
From Table
The above are all valid answers;
But I'm Not sure if this would work?
select IsNull((
select MAX(DateModified)
from table
)
,
(
select MAX(DateDeleted)
from table
)
) as MaxOfEitherValue
from table
Edit 1:
Whilst in the shower this morning, I had another solution:
Solution 2:
select MAX(v) from (
select MAX(DateModified) as v from table
union all
select MAX(DateDeleted) as v from table
) as SubTable
Edit 3:
Damn it, just spotted this is the same solution as Alex k. sigh...
How to find the Latest Date from the columns from Multiple tables
e.g. if the Firstname is in Table1, Address is in Table2, Phone is in Table3:
When you are using with main SELECT statement while selecting other columns it is best written as :
SELECT Firstname
,Lastname
,Address
,PhoneNumber
,
,(SELECT max(T.date_col) from(select max(date_col1) AS date_col from Table1 Where ..
union
select max(date_col2) AS date_col from Table2 Where..
union
select max(date_col3) AS date_col from Table3 Where..
) AS T
) AS Last_Updated_Date
FROM Table T1
LEFT JOIN Table T2 ON T1.Common_Column=T2.Common_Column
LEFTJOIN Table T3 ON T1.Common_Column=T3.Common_Column