Sum of null columns in SQL - sql

I have a table where A, B and C allow null values.
SELECT
A, B, C,
A + B + C AS 'SUM'
FROM Table
If my A, B and C values are 10, NULL and NULL, my SUM column shows nothing in it.
Is there any way to fix this, and display SUM as 10? OTHER THAN converting NULL s to Zeros?

You could use SQL COALESCE which uses the value in the column, or an alternative value if the column is null
So
SUM ( COALESCE(A,0) + COALESCE(B,0) + COALESCE(C,0))

In this case you need to use IsNull(Column, 0) to ensure it is always 0 at minimum.
SELECT
A, B, C,
IsNull(A,0) + IsNull(B,0) + IsNull(C,0) AS 'SUM'
FROM Table
ISNULL() determines what to do when you have a null value. if column returns a null value so you specified a 0 to be returned instead.

Related

Compare nulll value with non null value SQL

I have two columns as below:
Column A
Column B
A1
NULL
A1
A1
B1
C1.
When i query these columns as below :
SELECT Column A, Column B
from table
where Column A != Column B
i am expecting the following result:
Column A
Column B
A1
NULL
B1.
C1
But my query is only giving me the second line as result.
Consider below
select *
from your_table
where columnA is distinct from columnB
with output
Null values do not participate in equality operations as there is no value to compare with.
You will need to include a check for null into your comparisoon statement.
SELECT Column A, Column B from table where ISNULL(Column A,0) <> ISNULL(Column B,0)
null in SQL equals unknown
It could also have A1 as value, therefore it will not show up if you check if A1 is not null
You can use below workaround
select * from your_table
except distinct
select * from your_table
where columnA = columnB
with output
SELECT * from Your_table where IFNULL(`Column A`, 'NULL') != IFNULL(`Column B`, 'NULL')
IFNULL(expression, alt_value) function returns the value of the expression if it's not NULL and returns alt_value when expression is NULL.
So, from the above query, Column A and Column B will have the string value of 'NULL' when the column has the NULL value so they can still be comparable.

SQL - "Case when" vs condition

I have a table Table_A with columns A, B and C whereby column C needs to be summed, but only if column B is a certain value. Otherwise column C may not always contain a value to be summed.
So the normal SQL of:
SELECT A, B, SUM(C)
FROM Table_A
WHERE B = 'value condition'
GROUP BY A,B
Works well. However, I thought I could use "CASE WHEN" to catch the conditions with zero, say, like this:
SELECT
A, B,
CASE WHEN B = 'value condition' THEN SUM(C) ELSE 0 END
FROM Table_A
GROUP BY A, B
I get an error, referring to the fact that it is still trying to SUM a value not which is not in the condition. Am I missing something? Or have I misinterpreted CASE WHEN?
You want conditional aggregation. The CASE expression should appear inside SUM:
SELECT A, SUM(CASE WHEN B = 'value condition' THEN C ELSE 0 END) AS total
FROM Table_A
GROUP BY A;
Note that B probably does not belong in the GROUP BY clause, given that you want to conditionally aggregate using its values.

how can I fill null value with S.T in PostgreSQL?

Does anyone know how to fill null value in PostgreSQL?
For example, Null values are replaced with X
column
A
B
Null
C
D
Null
should be:
column
A
B
x
C
D
x
you can use coalesce() to return a different value if a column is null
select coalesce(the_column, 'x') as the_column
from the_table;

Count every rows

i tried to count the value of every rows in MYSQL. But it only count the first row only. Can someone assist
First Query:
SELECT A, B, C
FROM [TEST].[dbo].[TEST3]
Result:
A B C
7 8 9
1 2 NULL
1 3 4
1 NULL 1
Count every rows but only the first row appear as result.
Query
SELECT COUNT (A + B + C)
FROM [TEST].[dbo].[TEST3]
Result:
2
It supposed to be 7+8+9 = 22
1+2+NULL = 3
etc.
Just take the sum of the columns directly:
SELECT A, B, C,
COALESCE(A, 0) + COALESCE(B, 0) COALESCE(C, 0) AS total
FROM [TEST].[dbo].[TEST3]
The reason why your current query using COUNT returns a single row is that COUNT is an aggregate function. In the absence of GROUP BY, you are telling SQL Server to return a count over the entire table.
This is what you want:
SELECT IFNULL(A,0)+IFNULL(B,0)+IFNULL(C,0)
FROM [TEST].[dbo].[TEST3]
You do not want to use COUNT() here. COUNT() is an aggregate function. It output once per group. In your case, the whole query will output only one value.
Moreover, adding NULL to anything will be NULL and COUNT() will ignore that. Therefore the output of your query is 2.
COUNT() is a aggregate function which will return group result.
The result is actually correct since 1 + 2 + NULL = NULL, not 3.
SELECT COUNT (A + B + C) FROM [TEST].[dbo].[TEST3]
Returns 2 because COUNT() will count only non-null value. If you run the query without COUNT() it will return 4 rows.
SELECT A + B + C FROM [TEST].[dbo].[TEST3]
The result is
24
NULL
8
NULL
However, if you wanted to return rows considering NULL as 0, you can use COALESCE within the columns,
SELECT COALESCE(A, 0) + COALESCE(B, 0) + COALESCE(C, 0)
FROM [TEST].[dbo].[TEST3]
will now return
24
3
8
2
And when you write it with count, it will now return 4.
SELECT COUNT(COALESCE(A, 0) + COALESCE(B, 0) + COALESCE(C, 0) )
FROM [TEST].[dbo].[TEST3]
Result:
4
Here's a Demo.
SELECT
A, B, C,
Total = A + B+ C
FROM dbo.TEST
DO NOT USE COUNT.

Combine two (or multiple) columns of a table

I have a table
a b c
1 2
1 3
1 4 1
2 1 2
The column a and c should be combined if the value is the same. If there are not the same, it is always so that one is empty
So the result should be:
a b
1 2
1 3
1 4
2 1
Is there any function that can be applied in PostgreSQL?
According to your description:
The column a and c should be combined if the value is the same. If
there are not the same, it is always so that one is empty
all you need is an unconditional COALESCE.
SELECT COALESCE(a, c) AS a, b FROM tbl;
Assuming that by "empty" you mean NULL, not an empty string (''), in which case you'd add NULLIF:
SELECT COALESCE(NULLIF(a, ''), c) AS a, b FROM tbl;
COALESCE works for multiple parameters:
SELECT COALESCE(a, c, d, e, f, g) AS a, b FROM tbl;
Are you looking for something like this?
SELECT COALESCE(c, a), b
FROM your_table
WHERE COALESCE(c, a) = a