简体   繁体   中英

Grouping a sum within a sql query to aggregate the rows

I'm having trouble with a SQL query. I have a few tables that are linked together, and I'm trying to group/aggregate the returns so they make sense (I'm terrible at grouping in SQL)

Here are my table structures with test data:

Table InsuranceCompanies (InsuranceCompanyID, CompanyName)

1  InsuranceCompany1
2  InsuranceCompany2

Table InsurancePlans (InsurancePlanID, PlanName)

1  InsurancePlan1
2  InsurancePlan2

Table Practices (PracticeID, PracticeName)

1   Practice1

Table PracticesToInsuranceCompanies (PracticeID, InsuranceCompanyID)

1   1
1   2

Table PracticesToInsurancePlans (PracticeID, InsurancePlanID, SubCount)

1   1   5
1   2   10

Here is my current query:

select 
    p.Name,
    COUNT(ptc.InsuranceCompanyID) as NumberInsuranceCompanies,
    isnull(ptp.SubCount), 0) as SubCount
from 
    Practices p
left outer join 
    PracticesToInsuranceCompanies ptc on ptc.PracticeID = p.PracticeID
left outer join 
    PracticesToInsurancePlans ptp on ptp.PracticeID = p.PracticeID
group by 
    p.Name, ptp.SubCount
order by 
    p.Name asc

Here is the current result set:

RESULTS (PracticeName, NumberInsuranceCompanies, SubCount)
Practice1   2   10
Practice1   2   5

In the above example, the INTENDED result is to have one single row, since there is only one Practice being returned. The practice has two plans associated with it, one with a Subcount of 10, one with Subcount of 5, I just need that row aggregated into one row with the SubCount added as a sum. The number of insurance companies is just the count of how many are associated with it.

INTENDED RESULTS
Practice1  2   15

There is two things you want to see per practice: the number of insurence companies, if any, and the number of subcounts, if any.

The problem is that once you join both other tables to the practices table you get records multiplied (eg 1 practice with 2 ptc and 3 ptp make 6 records).

The easiest way to get what you want is not to join at all but use subqueries in your select clause:

select 
  Name,
  (
    select count(*) 
    from PracticesToInsuranceCompanies ptc 
    where ptc.PracticeID = p.PracticeID
  ) as NumberInsuranceCompanies,
  (
    select isnull(sum(SubCount), 0)
    from PracticesToInsurancePlans ptp
    where ptp.PracticeID = p.PracticeID
  ) as SubCount
from Practices p;

Remove subcount from group by and use sum on subcount in the select clause.

   select p.Name,
   COUNT(ptc.InsuranceCompanyID) as NumberInsuranceCompanies,
   sum(isnull(ptp.SubCount, 0)) as SubCount
   from Practices p
   left outer join PracticesToInsuranceCompanies ptc on ptc.PracticeID = p.PracticeID
   left outer join PracticesToInsurancePlans ptp on ptp.PracticeID = p.PracticeID
   group by p.Name
   order by p.Name asc

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