Count every rows - sql

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.

Related

How to sum non-null values from multiple columns (workaround for CASE WHEN limitation) Postgresql

So I essentially want to work around the fact that CASE WHEN stops executing when it finds its first TRUE return.
I'd like to sum every instance of a non-null value between multiple columns, and group these based on my ID. Example table:
id
input1
input2
input3
1
a
null
k
2
null
null
b
3
null
null
null
4
q
null
r
5
x
p
j
6
null
y
q
I would like the output of my function to be:
id
total_inputs
1
2
2
1
3
0
4
2
5
3
6
2
Any work arounds? Is a custom function in order to create a count of unique or non-null entries across multiple columns, grouped by row?
I know I can create a CTE and assign 1's to each non-null column but that seems tedious (my data set has 39 inputs) - and I'd like to have a reusable function I could use again in the future.
You could use a simple aggregation as the following:
Select id,
Count(input1) + Count(input2) + Count(input3) As total_inputs
From table_name
Group By id
Order By id
Noting that Count(inputX) = 0, where inputX is null.
See a demo.
We can simply use:
select ID,
case when input1 is not null then 1 else 0 end
+ case when input2 is not null then 1 else 0 end
+ ...
+ case when input39 is not null then 1 else 0 end as total_inputs
from ...
No need to group by if you want every row (or count, we are not aggregating rows - that is what COUNT()..GROUP BY is for), or CTE.
Also, for some PostgreSQL versions, there is a num_nulls function to count null parameters:
select
, 32-num_nulls(input1, input2, input3, ..., input32)

SQL adding numbers while handling nulls

I have this statement here:
SELECT sum(table1.foo + table1.bar) AS Sum
FROM table1
GROUP BY Fname;
When I try to add the numbers from foo and bar if one value from foo or bar is null it throws the numbers and gives me a different count sum
foo | bar
6 4
5 null
9 1
2 1
3 null
I want it to add all the numbers giving me a total of 31
but in this case it gives me a total of 23
Would love some help! Thanks!
Use coalesce():
SELECT sum(coalesce(table1.foo, 0) + coalesce(table1.bar, 0)) AS Sum
FROM table1
GROUP BY Fname;
If you want the total, total on one row, remove the group by:
SELECT sum(coalesce(table1.foo, 0) + coalesce(table1.bar, 0)) AS Sum
FROM table1;
Any Number + NULL = NULL. You want to indicate to the engine that when it sees a NULL, it should treat that NULL value as 0.
SELECT sum(ISNULL(table1.foo, 0) + ISNULL(table1.bar,0)) AS Sum
FROM table1
GROUP BY Fname;
I believe you'll need to have SQL replace nulls with a zero using "ISNULL".
Try:
SELECT sum(ISNULL(fix_bat_sum.foo, 0) + ISNULL(fix_bat_sum.bar, 0) ) AS Sum

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

Sum of null columns in 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.

Select rows until condition met

I would like to write an Oracle query which returns a specific set of information. Using the table below, if given an id, it will return the id and value of B. Also, if B=T, it will return the next row as well. If that next row has a B=T, it will return that, and so on until a F is encountered.
So, given 3 it would just return one row: (3,F). Given 4 it would return 3 rows: ((4,T),(5,T),(6,F))
id B
1 F
2 F
3 F
4 T
5 T
6 F
7 T
8 F
Thank you in advance!
Use a sub-query to find out at what point you should stop, then return all row from your starting point to the calculated stop point.
SELECT
*
FROM
yourTable
WHERE
id >= 4
AND id <= (SELECT MIN(id) FROM yourTable WHERE b = 'F' AND id >= 4)
Note, this assumes that the last record is always an 'F'. You can deal with the last record being a 'T' using a COALESCE.
SELECT
*
FROM
yourTable
WHERE
id >= 4
AND id <= COALESCE(
(SELECT MIN(id) FROM yourTable WHERE b = 'F' AND id >= 4),
(SELECT MAX(id) FROM yourTable )
)