I have query subjects, Case and Person. A person belongs to a case if the case id appears in the person record. I'm trying to get a count of people by case id without double counting.
Person Fields: personID (PK), caseID (FK)
Case Fields: caseID (PK)
I can create a query item in Case that looks something like this: count( [Person].[personID] for [Case.caseID] )
When I test it, I see my count and the caseID appear once for each person on the case. If three people are on a case, I get the corresponding caseID and "3" three times.
Try
count(distinct [Person].[personID] for [Case.caseID] )
. You may also be able to get away withcount(distinct [Person].[personID])
depending on how your list is configured.Alright, try this. It sounds more and more like a display issue instead of a query problem.
I'm assuming you're on Cognos BI 8.3, 8.4, 10.1, or 10.2. I'm also assuming you're using Report Studio and not Query Studio. I'm used to 8.4.1, but these are all pretty similar from what I can tell. If you're on Cognos Insight, then we're not going to communicate at all. I translated some reports from that, but that's all.
Make a new list report with a new query. Add three data items:
CaseID
,PersonID
, andPersonID
a second time (I'll refer to it asPersonID1
, as that's what Cognos will probably call it). Just add them from the Person table for now, and don't modify the data item to add thecount()
function at all.Now, add this query as a list to your report and run it. You should see a pretty basic list that would match a very simple query in SQL.
Go back to your query, and edit the data item for
PersonID1
to becount (distinct [PersonID] for [CaseID])
.Go back to the list on Page1. This time, group the list by
CaseID
. (ClickCaseID
on the List (not the list header), then go Structure -> Group). Now when you run the report, you should see that theCaseID
field will span multiple rows where it makes sense to, but thePersonID
andPersonID1
fields don't.That is what I'm assuming you see. I'm assuming you don't like that
PersonID1
repeats over and over again.Now, the real easy thing is to say, "Oh, I'll group by
PersonID1
and that will work!" Well, no, that's not what you want to do. It might work, but it won't work reliably. Instead, go back once more to Page1, and click on thePersonID1
data item (not the list header). Look at the properties window for this item. Under "Data", you should see "Group Span". Click that, and set the drop-down menu to what should be the only available group,CaseID
. Now when you run the report, you should get a single item with a cell that spans rows.Is that what you're looking for, or am I going too simple now?
For more complex reports, you'll need to have more control over Grouping and Sorting, controlling the order of both. You can get back to that by clicking the List, then going Data -> Advanced Sorting. Here you can specify how data is grouped and sorted, and make corrections or adjustments as necessary.