Query select all fields are not null - mysql

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

Related

How to Select count() inside select statement

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

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

Sum of 2 fetched columns in other column in Big query SQL

select x,
count(case when i='abc' then 1 else null end) as ele1,
count(case when i='def' then 1 else null end) as ele2,
sum(ele1+ele2) as sum1 from (INNER QUERY)
When i am using sum(ele1+ele2), it is throwing error that ele1 not found. How to fetch sum1 in the same query without using any other outer query?
You can't use alias as column name in select
select x,
count(case when i='abc' then 1 else null end) as ele1,
count(case when i='def' then 1 else null end) as ele2,
sum( ( case when i='abc' then 1 else null end ) +
( case when i='def' then 1 else null end ) ) as sum1
from (INNER QUERY)
You cannot use alias as a column name, but if your concern is in verboseness - in your particular case you can write something like below, which is easy readable and skinny enough (for BigQuery Legacy SQL)
SELECT
SUM( i ='abc' ) AS ele1,
SUM( i = 'def' ) AS ele2,
SUM( i IN ('abc', 'def') ) AS sum1
FROM (INNER QUERY)

sql server null value in where clause

There is one table which is having only one row with 4 date columns ,initially all date value are null so
if Exists(select 1 from rep_master where pacdt_1=null OR
pacdt_2=null OR
pacdt_3=null OR
pacdt_4=null)
select 0
else
select 1
this one is returning 1
if Exists(select 1 from rep_master where ISNULL(pacdt_1,0)=0 or
ISNULL(pacdt_2,0)=0 or
ISNULL(pacdt_3,0)=0 or
ISNULL(pacdt_4,0)=0 )
select 0
else
select 1
this one is returning 0 ,which is correct result
I m unable to figure out what is wrong with first query?
use IS NULL rather than = NULL
so:
if Exists(select 1 from rep_master
where pacdt_1 is null OR pacdt_2 is null OR
pacdt_3 is null OR pacdt_4 is null)
select 0
else
select 1
You can't equal null, you have to use IS
if Exists(select 1 from rep_master where pacdt_1 IS null OR
pacdt_2 IS NULL OR
pacdt_3 IS NULL OR
pacdt_4 IS NULL)
select 0
else
select 1
to get where the value is not null you use IS NOT
if Exists(select 1 from rep_master where pacdt_1 IS NOT null OR
pacdt_2 IS NOT NULL OR
pacdt_3 IS NOT NULL OR
pacdt_4 IS NOT NULL)
select 0
else
select 1

MySQL I dont want my SUM CASE to be Null

I am using a MySQL database and I have a big mysql_query that works completely fine except for this one line.
SELECT game_id,
COALESCE(
SUM(CASE WHEN game_score = 1 THEN 1 ELSE 0 END)
-
SUM(CASE WHEN game_score = 0 THEN 1 ELSE 0 END), 0) AS score
FROM ....
WHERE ....
GROUP BY ....
This line returns me almost all of the numbers that I want, yet they are the same numbers as when I wasn't using the COALESCE function.
I would like this statement to return me 0 when there is a game_id that has an emptygame_score` field.
Where is my code going wrong?
you can also use decode function like this ---
SELECT game_id,
SUM(DECODE(game_score, null, 0, 0, -1, 1, 0)) AS score
FROM ....
WHERE ....
GROUP BY ....
I have checked this query for working, its fine
hope it helps you....
add
when game_score is null then ...
Edit
SELECT game_id, case when game_score is null then 0 else
SUM(CASE WHEN game_score = 1 THEN 1 ELSE 0 END)
-
SUM(CASE WHEN game_score = 0 THEN 1 ELSE 0 END) end AS score
FROM games
group by ...
See this SQLFiddle Example
Use is null like
CASE WHEN game_score = 1 THEN 1
when game_score is null THEN 0
ELSE 0 END
The problem is that game_id can have an empty game_score field in one row and a valid one in another. Your code picks up the valid one and returns non-null.
Fix this problem by looking for this case explicitly, as the first argument to coalesce:
SELECT game_id,
(CASE WHEN count(*) <> count(game_score) then NULL
ELSE COALESCE(SUM(CASE WHEN game_score = 1 THEN 1 ELSE 0 END),
. . .
I'm not sure what the coalesce is trying to do. It seems to be adding one for a bunch of different game_scores. Can you use one of the following instead:
COUNT(game_score) ?
SUM(case when game_score between 1 and xx then 1 else 0 end) ?
Use the COALESCE function around your field name in your statements instead of surrounding the SUM function:
SUM(CASE COALESCE(game_score,0) WHEN 1 THEN 1 ELSE 0 END) - SUM(CASE COALESCE(game_score,0) WHEN 0 THEN 1 ELSE 0 END)