[SalesForce] Querying distinct logins using LoginHistory and COUNT_DISTINCT

I seem to have run in to a SOQL error that I'm hoping someone has solved before.

I am creating a dashboard for UserLogins using the LoginHistory object

The query I am trying to run is:

SELECT COUNT_DISTINCT(userid) agg 
FROM loginhistory 
WHERE logintime = THIS_MONTH 
AND userid IN ('id1', 'id2', ...)

When I run the query I get back this agg result:

[ {
  "attributes" : {
    "type" : "AggregateResult"
  },
  "agg" : 1
} ]

What I need is the number of distinct logins within the time frame, however when I add a GROUP BY UserID salesforce errors:

"Grouped field should not be aggregated: UserId"

Query:

SELECT userid,COUNT_DISTINCT(userid) agg 
FROM loginhistory 
WHERE logintime = TODAY 
AND userid IN ('id1','id1') 
GROUP BY userid

Best Answer

SELECT Count(Id) agg, userId acc 
FROM LoginHistory 
GROUP BY UserId
Related Topic