在sql中总结两个计数

时间:2015-11-26 10:38:52

标签: sql sql-server

我有问题,我正在建立一个查询。

select top 10 usr.firstname, usr.lastname, count(doc.modifiedBy) as modified_items,
       count(doc.createdDate) as created_items 
from document doc
where doc.active = 't' 
group by usr.firstname, usr.lastname
order by doc.modified_items desc

但是我想总结两个选择的计数,就像这样。

select top 10 usr.firstname, usr.lastname, sum(count(doc.modifiedBy), count(doc.createdDate))

1 个答案:

答案 0 :(得分:4)

简单只需添加+

SELECT TOP 10 usr.firstname, usr.lastname, count(doc.modifiedBy) AS modified_items, count(doc.createdDate) AS created_items, (count(doc.modifiedBy)) + (count(doc.createdDate)) AS summed_items
FROM document doc
WHERE doc.active = 't' 
GROUP BY usr.firstname, usr.lastname
ORDER BY doc.modified_items DESC