简体   繁体   中英

Need SQL query to find count

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 .

SELECT COUNT(*)
FROM 
(
    SELECT Household 
    FROM   TableName 
    GROUP BY Household 
    HAVING COUNT(Household) > 4
) AS T        

Maybe something like this:

SELECT COUNT(Household)
FROM
(
 SELECT Household
 FROM TABLE1
 GROUP BY Household
 HAVING Count(Household) > 4
) Tmp

FIDDLE DEMO

SELECT Household, count(Household) as cnt FROM {{TABLE}} 
GROUP BY Household
WHERE cnt > 4
SELECT household FROM YourTable
GROUP BY household
HAVING COUNT(DISTINCT individual) > 4

如果我正确理解了您的问题,这应该为您提供正确的值(1)。

select count(*) from (select Household  from table group by Household  having count(*)>4)
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

Initial Answer

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:

  1. Select the COUNT s of Individual s in the inner query.
  2. Group the COUNT s of Individual s on Household values.
  3. Use the aggregate equivalent of WHERE - HAVING - to filter the inner query's results (ie > 4 ).
  4. Select the final COUNT in the outer query.

Follow-On

@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 (ie that the 1st query I provided would yield 2 in this case whereas the 2nd query I provided would still yield 1).

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM