我正在尝试做一些非常简单的事情,但是我在做错事情上是空白。我有一个查询,该查询返回一个姓氏,一个ID以及该姓氏查看ID的次数。我只需要一行结果来计算查看了多少个不同的ID。
很抱歉,如果您之前曾询问过此问题,我曾尝试查找,但可能没有搜索正确的术语。
Select a.employeeLastName, COUNT(Distinct d.id) as counter, COUNT(a.employeeId) as avCount
From tblEmployees a
LEFT JOIN tblEvents b on b.employeeId = a.employeeId
LEFT JOIN tblEventsType c on c.typeId = b.typeId
LEFT JOIN assets d on d.id = b.assetId
WHERE a.employeeId = 138050
AND d.type = 'Article'
and c.typeId = 40
group by a.employeeId, d.id, a.employeeLastName
ORDER BY avCount DESC
该代码的结果是每个id一行。我需要计算所有ID返回的一行。
编辑: 新结果 empID计数器avCount 138050 1 24 138050 1 23 138050 1 20 138050 1 14 138050 1 7
答案 0 :(得分:1)
您只需要group by a.employeeId, a.employeeLastName
并计算不同的ID:
Select a.employeeId, a.employeeLastName, COUNT(DISTINCT d.id) as counter
From tblEmployees a
LEFT JOIN tblEvents b on b.employeeId = a.employeeId
LEFT JOIN tblEventsType c on c.typeId = b.typeId
LEFT JOIN assets d on d.id = b.assetId
WHERE a.employeeId = 138050
AND d.type = 'Article'
and c.typeId = 40
group by a.employeeId, a.employeeLastName