How to Select count() inside select statement - mysql

After hours of trying, i have finally come to terms i need assistance.
I'm trying to select a row from TableA and then count the number of cells of that row that are not empty.
I know this is faulty but it communicates my intention
SELECT COUNT(colName),
(SELECT (column1, column2, column3, column4) AS colName
FROM TableA
WHERE location= location)
AS colCount
FROM TableA
WHERE colName IS NOT NULL

SQL DEMO
SELECT id,
(`column1` IS NOT NULL) +
(`column2` IS NOT NULL) +
(`column3` IS NOT NULL) as notnull
FROM Table1

There may be a slicker way but a brute-force way would be:
SELECT
location,
CASE WHEN column1 IS NULL THEN 0 ELSE 1 END +
CASE WHEN column2 IS NULL THEN 0 ELSE 1 END +
CASE WHEN column3 IS NULL THEN 0 ELSE 1 END +
CASE WHEN column4 IS NULL THEN 0 ELSE 1 END
AS colCount
FROM TableA

select location,
sum(case when column1 is not null then 1 else 0 end) +
sum(case when column2 is not null then 1 else 0 end) +
sum(case when column3 is not null then 1 else 0 end) +
sum(case when column4 is not null then 1 else 0 end) cnt
from TableA
group by location

Related

How to get count of columns that are having null values for a given row in sql?

I have a table that are having 115 columns.
Out of 7 columns I need to get the count of columns that are having not null values for a given row.
One method is to use case and +:
select t.*,
( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col5 is not null then 1 else 0 end) +
(case when col6 is not null then 1 else 0 end) +
(case when col7 is not null then 1 else 0 end)
) as cnt_not_nulls_in_row
from t;
In MySQL, this can be simplified to:
select t.*,
( (col1 is not null ) +
(col2 is not null ) +
(col3 is not null ) +
(col4 is not null ) +
(col5 is not null ) +
(col6 is not null ) +
(col7 is not null )
) as cnt_not_nulls_in_row
from t;
You may first query the given row from the table using the primary key and the use COUNT to count the number of columns from the queried row having null value, as follows:
WITH derived_row as
(SELECT col1, col2, col3, col4, col5, col6, col7 FROM table WHERE primary_key=key)
SELECT COUNT(CASE
WHEN col1 IS NULL THEN 1
WHEN col2 IS NULL THEN 1
WHEN col3 IS NULL THEN 1
WHEN col4 IS NULL THEN 1
WHEN col5 IS NULL THEN 1
WHEN col6 IS NULL THEN 1
WHEN col7 IS NULL THEN 1
END) AS null_column_count
FROM derived_row;

Row counts Sql query

I want result in Sql query
id1 id2 id3 Count Id
A001 A001 A001 3
A001 NULL A001 2
A001 NULL NULL 1
In SQL Server, i would use VALUES construct :
SELECT t.*,
(SELECT COUNT(tt.ids) FROM ( VALUES (t.id1), (t.id2), (t.id3) ) tt(ids)
) as Count_Id
FROM table t;
In standard SQL you can use CASE Expression :
SELECT t.*,
( (CASE WHEN ID1 IS NOT NULL THEN 1 ELSE 0 END) +
(CASE WHEN ID2 IS NOT NULL THEN 1 ELSE 0 END) +
(CASE WHEN ID3 IS NOT NULL THEN 1 ELSE 0 END)
) AS Count_Id
FROM table t
are you find something like below
select id1,id2,id3,
case when id1 is not null then 1 else 0 end+
case when id2 is not null then 1 else 0 end+
case when id3 is not null then 1 else 0 end as val

How to count non null fields?

I need to get the count of fields where the value is not null.
My table
city id_no no1 no2 no3
chn A12 2158
chn A13 8181 8182 8183
chn A14 19138
I need to get the count of fields set for no1, ..., no3
My query
SELECT
count(id_no) as total_id,
(count(no1) +
count(no2) +
count(no3)) as c_count
FROM table
WHERE city='chn';
My output
total_id c_count
3 9
Expected:
total_id c_count
3 5
I am expecting 5 instead of 9, since 5 fields are not null.
OR you can simply do this to avoid NULL or ' ' data
SELECT
count(id_no) as total_id,
(count(CASE WHEN no1 > 0 THEN no1 ELSE NULL END) +
count(CASE WHEN no2 > 0 THEN no2 ELSE NULL END) +
count(CASE WHEN no3 > 0 THEN no3 ELSE NULL END)) as c_count
FROM table
WHERE city='chn';
SELECT
count(id_no) as total_id,
(case when count(no1)='' or count(no1) is null then 0 else count(no1) end +
case when count(no2)='' or count(no2) is null then 0 else count(no2) end +
case when count(no3)='' or count(no3) is null then 0 else count(no3) end +
case when count(no4)='' or count(no4) is null then 0 else count(no4) end +
case when count(no5)='' or count(no5) is null then 0 else count(no5) end +
case when count(no6)='' or count(no6) is null then 0 else count(no6) end +
case when count(no7)='' or count(no7) is null then 0 else count(no7) end +
case when count(no8)='' or count(no8) is null then 0 else count(no8) end +
case when count(no9)='' or count(no9) is null then 0 else count(no9) end +
case when count(no10)='' or count(no10) is null then 0 else count(no10) end
) as c_count
FROM table
WHERE city='chn';
I am getting same output as you want please check here i provide my screenshot
SELECT count(id_no) as total_id,
count(CASE WHEN `nol`!="" THEN 1 END) as no1
FROM `table` where city='chn'
try like this
select count(distinct a.`id_no`),count(*) from
(
select `id_no`,`no1` as `non` from table WHERE city='chn'
union all
select `id_no`,`no2` as `non` from table WHERE city='chn'
union all
select `id_no`,`no3` as `non` from table WHERE city='chn'
)a where a.`non` is not null

Query select all fields are not null

I have a table that I want to count number of fields which their field is not null. How I can return the result?
SELECT * FROM `fakelos2` WHERE fields are not Null
Instead of are use is, while to count rows for particular field, you use count function like below:
SELECT count(*)
FROM fakelos2
WHERE fields is not null
Now you have to count each and every field then you have to use case when then like below:
SELECT
((CASE WHEN field1 IS NULL THEN 1 ELSE 0 END)
+ (CASE WHEN field2 IS NULL THEN 1 ELSE 0 END)
+ (CASE WHEN field3 IS NULL THEN 1 ELSE 0 END)
...
...
+ (CASE WHEN field10 IS NULL THEN 1 ELSE 0 END)) AS sum_of_nulls
FROM fakelos2

I want to display one columns data sum on different columns based on where clause

Below is image which describe my requirements i have a column having different data i want its count on where condition where data=1 as col1 and where data = 2 as col2 and so on....
SELECT
COUNT(CASE data WHEN 1 THEN 1 ELSE NULL END) as Col1,
COUNT(CASE data WHEN 2 THEN 1 ELSE NULL END) as Col2,
COUNT(CASE data WHEN 3 THEN 1 ELSE NULL END) as Col3,
COUNT(CASE data WHEN 4 THEN 1 ELSE NULL END) as Col4
FROM yourTable
SELECT SUM(CASE WHEN data=1 THEN 1 ELSE 0 END) AS col1,
SUM(CASE WHEN data=2 THEN 1 ELSE 0 END) AS col2
FROM Table
Link to SQL Fiddle Example
SELECT
COUNT(CASE data WHEN 1 THEN 1 ELSE NULL END) as Col1,
COUNT(CASE data WHEN 2 THEN 1 ELSE NULL END) as Col2,
COUNT(CASE data WHEN 3 THEN 1 ELSE NULL END) as Col3,
COUNT(CASE data WHEN 4 THEN 1 ELSE NULL END) as Col4
FROM yourTable