0

这是我到目前为止所拥有的:

SELECT p2.fullname[UploadedBy], p.fullname[UploadedTo]
from docexchange d (NOLOCK)

left join exchange_permissions r on r.file_id = d.id
inner join people p on r.people_id = p.id
inner join people p2 on d.upload_by_id = p2.id

where upload_date > '2013-05-21'

这个查询返回大约 8000 个结果——如果我添加DISTINCT到我的SELECT语句中,它返回大约 900 个结果——我想得到这 900 个结果,但我需要另一个列来告诉我这些结果在数据库中出现了多少次。

我试过这样做:

Select p2.fullname, p.fullname, COUNT(DISTINCT p2.fullname + p.fullname)

但这会返回错误:选择列表中的“people.fullname”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

4

1 回答 1

2

你想要一个group by

SELECT p2.fullname as [UploadedBy], p.fullname as [UploadedTo], count(*)
from docexchange d (NOLOCK)

left join exchange_permissions r on r.file_id = d.id
inner join people p on r.people_id = p.id
inner join people p2 on d.upload_by_id = p2.id

where upload_date > '2013-05-21'
group by p2.fullname, p.fullname
于 2013-06-21T18:24:42.083 回答