I need to get column count whose value greater than 40
example :
id col_1 col_2 col_3
1 20 60 80
The output like this count=2
query:
select count(columns) from table where id =1 and col_values >40;
How to write query to get this please help me to write query.
Your database is not normlized! Please, normalize it.
Try this:
SELECT SUM(
CASE
WHEN col1 >= 40 THEN 1 ELSE 0
END +
CASE
WHEN col2 >= 40 THEN 1 ELSE 0
END +
CASE
WHEN col3 >= 40 THEN 1 ELSE 0
END)
FROM yourtable
With this query you summarize all columns and you'll get only sum trasversal by rows.
So, if you have this result set
id col_1 col_2 col_3
1 20 60 80
2 44 22 20
You'll get 3 (two from row with id 1 and 1 for row with id 2)
EDIT
If you want get this information only for one row you can integrate you query with id = value
(as in your question) so the query becomes:
SELECT SUM(
CASE
WHEN col1 >= 40 THEN 1 ELSE 0
END +
CASE
WHEN col2 >= 40 THEN 1 ELSE 0
END +
CASE
WHEN col3 >= 40 THEN 1 ELSE 0
END)
FROM yourtable
WHERE id = 1
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.