SQL query issue COUNT() - sql

I tried set the count of rows which have a specific value for multiple columns in the same query
this is what I have tried...
SELECT
COUNT(*) AS house_with_12_rooms,
COUNT(*) AS house_with_4_rooms
FROM housetable
WHERE num_of_rooms = "twelve"
OR num_of_rooms = "four"
since there are 157 rows with houses with 12 rooms and 1 row with houses with 4 rooms the result adds up the counts in the result table RATHER THAN placing 157 and 1 respectively.
Row house_with_12_rooms house_with_4_rooms
1 158 158
Could someone give me a clue on how to set up the query properly?

Use conditional aggregation:
SELECT SUM(CASE WHEN num_of_rooms = 'twelve' THEN 1 ELSE 0 END) AS house_with_12_rooms,
SUM(CASE WHEN num_of_rooms = 'four' THEN 1 ELSE 0 END) AS house_with_4_rooms
FROM housetable
WHERE num_of_rooms IN ('twelve', 'four')

Related

Counting values within the same row in SQL Server

My SQL Server problem is as follows: let's say we have clients and we wish to rate them based on 4 categories (Score_1...Score_4) on a scale of 0 to 2. I have a table presented below:
What I want my code to do is count the number of 0, 1, and 2 values each of my clients recieved. The result table I would like to get would like this:
So client_1 got two 0 scores, one 1 score and one 2 score, client_2 got one 0 score, one 1 score and two 2 scores. Any suggestions? Thanks in advance!
You can unpivot, then do conditional aggregation:
select t.id,
sum(case when x.score = 0 then 1 else 0 end) cnt_0,
sum(case when x.score = 1 then 1 else 0 end) cnt_1,
sum(case when x.score = 2 then 1 else 0 end) cnt_2
from mytable t
cross apply (values (score_1), (score_2), (score_3), (score_4)) x(score)
group by t.id

Running Counts on Multiple Columns with One SQL Query

I'm looking to run a count SQL query on multiple columns at once. 83 of them. For each, I'd simply like to figure out how many instances there are in which the value = 1.
ex.
select count(*) from [filename.filename]
where [Column001] = 1
select count(*) from [filename.filename]
where [Column002] = 1
All data in each column is marked with wither a 0 or a 1.
Instead of writing 83 small queries, is there a way for me to write it all in one query and have them all display as a table with the results?
This seems to be what you want:
SELECT SUM(CASE WHEN Column_1 = 1 THEN 1 ELSE 0 END) N_1,
SUM(CASE WHEN Column_2 = 1 THEN 1 ELSE 0 END) N_2,
SUM(CASE WHEN Column_3 = 1 THEN 1 ELSE 0 END) N_3,
.....
SUM(CASE WHEN Column_83 = 1 THEN 1 ELSE 0 END) N_83
FROM YourTable;

Select Count Where Values greater than 0 SQL Server

I'm trying to figure out how to return a select query showing a count of all values in a column that are greater than 0. Then in the next column show a count of all values that = 0.
Example:
ID ColumnA
1 1
2 2
3 1
4 2
5 0
6 0
7 1
Would return a result for the select query of:
NumberOfGreaterThan0 NumberThatEqual0
5 2
You can use conditional aggregates for this via CASE expression:
SELECT COUNT(CASE WHEN ColumnA > 0 THEN 1 END) AS NumberOfGreaterThan0
,COUNT(CASE WHEN ColumnA = 0 THEN 1 END) AS NumberThatEqual0
FROM YourTable
This works because aggregate functions ignore NULL values.
You can use a couple of count functions over case expressions:
SELECT COUNT(CASE WHEN columa > 0 THEN 1 ELSE NULL END) AS NumberOfGreaterThan0,
COUNT(CASE columa WHEN 0 THEN 1 ELSE NULL END) AS NumberThatEqual0
FROM my_table

Nested SQL Queries: How to get multiple counts on same elements based on different criteria

I have a table like this :
Server CompliancePercentage
A 25
B 15
C 45
D 75
E 17
F 82
I want to get from a single query the results in the following way:
Conformity% 00-20 20-40 40-60 60-80 80-100
Server Count 2 1 1 1 1
How do I get the above mentioned result from a nested query ?
Any help would be greatful.
Thanks a lot in advance.
Suvi
You can use an aggregate function with a CASE expression to get the result:
select
'ServerCount' Conformity,
count(case when CompliancePercentage >= 0 and CompliancePercentage <20 then 1 end) Per00_19,
count(case when CompliancePercentage >= 20 and CompliancePercentage <40 then 1 end) Per20_39,
count(case when CompliancePercentage >= 40 and CompliancePercentage <60 then 1 end) Per40_59,
count(case when CompliancePercentage >= 60 and CompliancePercentage <80 then 1 end) Per60_79,
count(case when CompliancePercentage >= 80 and CompliancePercentage <100 then 1 end) Per80_100
from yourtable;
See SQL Fiddle with Demo
Assuming the following table:
create table dummyTable
(
srv char(1),
comp int
)
You can write out the query similar to
select
[00-19] = (SELECT COUNT(1) FROM dummyTable where comp between 0 and 19),
[20-39] = (SELECT COUNT(1) FROM dummyTable where comp between 20 and 39)
If your table has a ton of records (ie, > 1M) you may want to consider adding a non-clustered index to the table on the compliance percentage column to avoid a bunch of table scans.
Something like that should works for you:
SELECT
( COUNT(id) FROM table WHERE id > 1 ) AS id_gt_1,
( COUNT(id) FROM table WHERE id > 100 ) AS id_gt_100
Here, the criteria I have matched is simply the count for the ID number.

Get the columns in a single query

I have a table like this
C1 C2
1 0
1 1
i want the count of rows where c1=1 and the count of rows where c2=0 in one single query instead of separate queries.
select
sum(case c1 when 1 then 1 else 0 end) Count_c1,
sum(case c2 when 0 then 1 else 0 end) Count_c2
from YourTable
You can use the UNION keyword:
SELECT "ones", COUNT(*) WHERE c1=1
UNOIN
SELECT "zeroes", COUNT(*) WHERE c2=0