I have an issue where the counts in my summary BAQ are not matching the details, and I can’t figure out why. I’m hoping some of the SQL experts here can help spot my (probably simple) mistake.

This BAQ will be used in a Dashboard (classic) to show Case counts by Topic over time. It takes in 2 parameters - StartDate & EndDate.

I put the BAQ together, got my results, then went to make sure they matched the count when I queried the case details and added up the count - and they didn’t. The summary query always returns more records than the detail query. I’ve tried adding DISTINCT to the InnerSubqueries and that doesn’t seem to make a difference.

Any help would be greatly appreciated. Here’s the BAQ. The first InnerSubquery is a technique I found on here to generate an on-the-fly date table.

```
select
[Dates].[Calculated_YearVal] as [Calculated_YearVal],
[Dates].[Calculated_MonthVal] as [Calculated_MonthVal],
[Dates].[Calculated_MonthName] as [Calculated_MonthName],
(COUNT(Complaints.HDCase_HDCaseNum)) as [Calculated_ComplaintCount],
(COUNT(CustomerInduced.HDCase1_HDCaseNum)) as [Calculated_CustomerInducedCount],
(COUNT(InternalIssues.HDCase2_HDCaseNum)) as [Calculated_InternalIssueCount],
(COUNT(Kudos.HDCase3_HDCaseNum)) as [Calculated_KudosCount],
(COUNT(SalesSupport.HDCase4_HDCaseNum)) as [Calculated_SalesSupportCount]
from (select distinct
((ROW_NUMBER() OVER(ORDER BY ZDataField.SysRevID ASC))) as [Calculated_Row_Num],
((DATEADD(DAY, Row_Num, '1/1/2020'))) as [Calculated_Date],
(datepart(year, Date)) as [Calculated_YearVal],
(datepart(month, Date)) as [Calculated_MonthVal],
(datename(month, Date)) as [Calculated_MonthName],
(datepart(wk, Date)) as [Calculated_WeekVal]
from Ice.ZDataField as ZDataField) as Dates
left outer join (select
[HDCase].[Company] as [HDCase_Company],
[HDCase].[HDCaseNum] as [HDCase_HDCaseNum],
[HDCase].[CreatedDate] as [HDCase_CreatedDate],
(datepart(year, HDCase.CreatedDate)) as [Calculated_YearVal],
(datepart(month, HDCase.CreatedDate)) as [Calculated_MonthVal],
(datename(month, HDCase.CreatedDate)) as [Calculated_MonthName]
from Erp.HDCase as HDCase
where (HDCase.TopicID1 = 'Complaint')) as Complaints on
Dates.Calculated_Date = Complaints.HDCase_CreatedDate
left outer join (select
[HDCase1].[Company] as [HDCase1_Company],
[HDCase1].[HDCaseNum] as [HDCase1_HDCaseNum],
[HDCase1].[CreatedDate] as [HDCase1_CreatedDate],
(datepart(year, HDCase1.CreatedDate)) as [Calculated_YearVal],
(datepart(month, HDCase1.CreatedDate)) as [Calculated_MonthVal],
(datename(month, HDCase1.CreatedDate)) as [Calculated_MonthName]
from Erp.HDCase as HDCase1
where (HDCase1.TopicID1 = 'Customer Induced')) as CustomerInduced on
Dates.Calculated_Date = CustomerInduced.HDCase1_CreatedDate
left outer join (select
[HDCase2].[Company] as [HDCase2_Company],
[HDCase2].[HDCaseNum] as [HDCase2_HDCaseNum],
[HDCase2].[CreatedDate] as [HDCase2_CreatedDate],
(datepart(year, HDCase2.CreatedDate)) as [Calculated_YearVal],
(datepart(month, HDCase2.CreatedDate)) as [Calculated_MonthVal],
(datename(month, HDCase2.CreatedDate)) as [Calculated_MonthName]
from Erp.HDCase as HDCase2
where (HDCase2.TopicID1 = 'Internal Issue')) as InternalIssues on
Dates.Calculated_Date = InternalIssues.HDCase2_CreatedDate
left outer join (select
[HDCase3].[Company] as [HDCase3_Company],
[HDCase3].[HDCaseNum] as [HDCase3_HDCaseNum],
[HDCase3].[CreatedDate] as [HDCase3_CreatedDate],
(datepart(year, HDCase3.CreatedDate)) as [Calculated_YearVal],
(datepart(month, HDCase3.CreatedDate)) as [Calculated_MonthVal],
(datename(month, HDCase3.CreatedDate)) as [Calculated_MonthName]
from Erp.HDCase as HDCase3
where (HDCase3.TopicID1 = 'Kudos')) as Kudos on
Dates.Calculated_Date = Kudos.HDCase3_CreatedDate
left outer join (select
[HDCase4].[Company] as [HDCase4_Company],
[HDCase4].[HDCaseNum] as [HDCase4_HDCaseNum],
[HDCase4].[CreatedDate] as [HDCase4_CreatedDate],
(datepart(year, HDCase4.CreatedDate)) as [Calculated_YearVal],
(datepart(month, HDCase4.CreatedDate)) as [Calculated_MonthVal],
(datename(month, HDCase4.CreatedDate)) as [Calculated_MonthName]
from Erp.HDCase as HDCase4
where (HDCase4.TopicID1 = 'Sales Support')) as SalesSupport on
Dates.Calculated_Date = SalesSupport.HDCase4_CreatedDate
where (Dates.Calculated_Date >= @StartDate and Dates.Calculated_Date <= @EndDate)
group by [Dates].[Calculated_YearVal],
[Dates].[Calculated_MonthVal],
[Dates].[Calculated_MonthName]
```

Cases-Summary.baq (95.5 KB)