Search code examples
sqljoingroup-byaggregate-functionshaving-clause

How to find group which is having Status Active?


I have a id which is having more than one Accountid in a table with status Active . I need to find group of active for the particular ID .

enter image description here

Please find my below query .is it correct or something need to modify to get better optimized result .

select id,count(Accountid) from CustomerAccount
where status='Active'
group by id
having count(*)>( select min(maxxount) from(
            select id,count(accountid) as maxxount
                        from CustomerAccount
                        group by id)A)

Solution

  • Use this query:

    select id,count(Accountid) from CustomerAccount
    where status='Active'
    group by id
    having count(*) >1