i have a database where i am storing integers in columns in a way like this
1 1 1 1
1 1 1 1
1 0 1 0
0 1 2 2
so i want to get count of each column where value is 1 for example.
I ll be very thankful :)
I have tried using many queries but i don't know how to work in this way I have also checked stack over flow and many other websites too but somehow i am still stuck at this problem. I have tried using IN clause too.
I have tried using select statement by using OR and AND but i knew that this will not work and it gives improper results.
It depends a bit on what you mean. If you want to count the number of times the value 1 appears in your tables, you can use count(*)
:
select
(select count(*) from myTable where col1 = 1) +
(select count(*) from myTable where col2 = 1) +
(select count(*) from myTable where col3 = 1) +
[...]
Or you could use case
expressions:
select sum(*) from (
select
(case when col1 = 1 then 1 else 0 end) +
(case when col2 = 1 then 1 else 0 end) +
(case when col3 = 1 then 1 else 0 end)
from myTable
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.