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
Related
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
I have a table below:
Item Status1 Status2
-----------------------------
A Good NULL
A Good NULL
A Good NULL
A Bad Good
B Bad Good
B Good NULL
C Good NULL
C Good NULL
C Good NULL
D Bad Good
Now, I'm thinking off writing a query which gives me the result below:
Item Good Bad
-----------------------------
A 4 1
B 2 1
C 3 0
D 1 1
Distinct in the Item column and the count of Good and Bad for each Item where NULL is not counted.
The column name can be of anything (I just kept it as Good and Bad in my second table).
Any suggestions/ideas on how to achieve my desired results?
Use UNION ALL & do aggregation :
select item, sum(status = 'good'), sum(status = 'bad')
from (select item, status1 as status
from table t
union all
select item, status2
from table t
) t
group by item;
You can use union all and conditional aggregation
select item, count(case when status1='good' then 1 end) as good,
count(case when status1='bad' then 1 end) as bad
from
(
select item , status1 from tablename
union all
select item , status2 from tablename
)A group by item
use union and case when
select Item, sum(case when status = 'good' then 1 else 0 end) as good,
sum ( case when status = 'bad' then 1 else 0 end) as bad
from (select Item, Status1 as status
from table_name
union all
select Item, Status2
from table_name
) t
group by Item;
There's no need for UNION, simply apply some logic.
select Item
,sum(case when Status1 = 'Good' then 1 else 0 end +
case when Status2 = 'Good' then 1 else 0 end) as good
,sum(case when Status1 = 'Bad' then 1 else 0 end +
case when Status2 = 'Bad' then 1 else 0 end) as bad
from tab
group by Item
or
select Item
,count(case when Status1 = 'Good' then 1 end) +
count(case when Status2 = 'Good' then 1 end) as good
,count(case when Status1 = 'Bad' then 1 end) +
count(case when Status2 = 'Bad' then 1 end) as good
from tab
group by Item
You can use sub-query and then apply sum function in outer query
select distinct(item) as item, sum(S1G+S2G) as Good,sum(S1B+S2B) as Bad from ( select item, CASE WHEN status1 ='Good' THEN 1 ELSE 0 END as S1G, CASE WHEN status2 ='Good' THEN 1 ELSE 0 END as S2G, CASE WHEN status2 ='Bad' THEN 1 ELSE 0 END as S2B, CASE WHEN status1 ='Bad' THEN 1 ELSE 0 END as S1B from t1 ) as b group by item
Here is demo
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
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 need to count the sum of the differences between a set of known data. The equivalent in php is
(count(array_diff_assoc($array1)). Is this possible with Mysql? I have a set of data to compare with each others field column in the table.
my data to compare is columnA : 3, columnB : 6, columnC : 4
the test table is :
ID columnA columnB columnC
1 2 6 1
2 6 1 3
3 3 6 4
result expected :
ID numbersofdifferences
3 0
1 2
2 3
thanks for any help,
Jesisca
This is not necessarily the cleanest but you could use an aggregate function with a CASE expression:
select id,
sum(case when columna = 3 then 0 else 1 end) +
sum(case when columnb = 6 then 0 else 1 end) +
sum(case when columnc = 4 then 0 else 1 end) TotalDiff
from yourtable
group by id;
See SQL Fiddle with Demo.
Edit, this could be done without the aggregate function as well:
select id,
(case when columna = 3 then 0 else 1 end) +
(case when columnb = 6 then 0 else 1 end) +
(case when columnc = 4 then 0 else 1 end) TotalDiff
from yourtable;
See Demo
There isn't a function built in to do something like that, but you could do it manually.
select
id,
case columnA when $columnA then 1 else 0 end +
case columnB when $columnB then 1 else 0 end +
case columnC when $columnC then 1 else 0 end differences
from
myTable
But if you want them in order, you'll want a subselect
select * from
(
select
id,
case columnA when $columnA then 1 else 0 end +
case columnB when $columnB then 1 else 0 end +
case columnC when $columnC then 1 else 0 end differences
from
myTable
) sqlRequiresNameHere
order by differences