Reputation: 11
Household Individual
101 11
101 12
101 13
101 14
101 15
102 1
102 2
102 3
102 4
I need a SQL query to get the number of households with more than 4 individuals. In the above table, only household 101
has more than 4 individuals. So the extract should be 1
.
Upvotes: 1
Views: 113
Reputation: 1723
I think you need:
SELECT COUNT(Household) AS HouseholdsWithMoreThan4Individuals
FROM (
SELECT Household
FROM [MytTable]
GROUP BY Household
HAVING COUNT(Individual) > 4) Res;
In this case the result will be 1 - the result you require - because there is only one household with more than 4 individuals.
You need to perform two operations: first you need to get a list of all households with more than 4 individuals. After that you need to count those households. You need to be careful not to count any household more than once.
Upvotes: 0
Reputation: 28403
Maybe something like this:
SELECT COUNT(Household)
FROM
(
SELECT Household
FROM TABLE1
GROUP BY Household
HAVING Count(Household) > 4
) Tmp
Upvotes: 1
Reputation: 69440
If I understand your question correctly, this should give you the right value (1).
select count(*) from (select Household from table group by Household having count(*)>4)
Upvotes: 0
Reputation: 8938
Of course a demonstration requires choosing a SQL implementation, my choice being T-SQL; but the solution is pretty generalizable to other SQL implementations:
declare @householdInfo table (Household int, Individual int);
insert into @householdInfo (Household, Individual)
select 101, 11
union
select 101, 12
union
select 101, 13
union
select 101, 14
union
select 101, 15
union
select 102, 1
union
select 102, 2
union
select 102, 3
union
select 102, 4;
--select * from @householdInfo; -- FORNOW: for sanity checking
-- NOTE: what you basically need - that yields 1 like you expect
select COUNT(*)
from (
select Household, COUNT(Individual) IndividualCount
from @householdInfo
group by Household
having COUNT(Individual) > 4) s;
The important parts are that you need to:
COUNT
s of Individual
s in the inner query.COUNT
s of Individual
s on Household
values.WHERE
- HAVING
- to filter the inner query's results (i.e. > 4
).COUNT
in the outer query.@VigneshKumar and @GiannisParaskevopoulos make a good point - that you technically do not need COUNT
in the inner query's column list.
Another (ugly) subtlety exists in the wild, may apply to your situation, and necessitates not only COUNT(Individual)
in the inner query('s HAVING
clause) but further leveraging a distinct count:
select COUNT(Household) -- yes, prefer Household over * for clarity
from (
select Household
from @householdInfo
group by Household
-- distinct added if duplicates are an issue
having COUNT(distinct Individual) > 4) s;
A simple change from the sample data you provided to...
Household Individual
--------- ----------
101 11
101 12
101 13
101 14
101 15
102 1
102 2
102 3
102 4
102 4 -- NOTE: duplicate row to demonstrate the point
...makes the distinction (pun intended) clear (i.e. that the 1st query I provided would yield 2 in this case whereas the 2nd query I provided would still yield 1).
Upvotes: 0
Reputation: 2940
select
distinct Household
from
Table_2
where
Individual > 4
Result will be 101.
If you need count (not number as above), then
select
COUNT (Household) as E1
from
Table_2
where
Individual > 4
Upvotes: 0
Reputation: 18411
SELECT COUNT(*)
FROM
(
SELECT Household
FROM TableName
GROUP BY Household
HAVING COUNT(Household) > 4
) AS T
Upvotes: 1
Reputation: 1062
SELECT household FROM YourTable
GROUP BY household
HAVING COUNT(DISTINCT individual) > 4
Upvotes: 0
Reputation: 33186
SELECT Household, count(Household) as cnt FROM {{TABLE}}
GROUP BY Household
WHERE cnt > 4
Upvotes: 0