Reputation: 29
How to sum count value in SQL Server ?
I have table 1. I want to sum count value.
How to do that ?
SELECT Top 10 count(d.name) as countname,d.name as name ,sum(count(d.name)) as sumcount
FROM table 1 as d
group by d.name order by count(d.name) desc
I want to display countname
, name
, sumcount
. How to do that ?
Upvotes: 1
Views: 34429
Reputation: 4266
Expanding on the answer provided by @Shark, MySQL syntax will look like the following:
set @TotalCount = (select sum(countname) from (
select count(d.name) as countname, d.name as name
from table 1 as d
group by name
order by countname desc
limit 10
) a);
select count(d.name) as countname, d.name as name, @TotalCount
FROM table 1 as d
group by name
order by countname desc
limit 10;
You may need to look up MS SQL syntax for setting local variables and limits.
Upvotes: 0
Reputation:
Not sure I'm understanding your question, but if you're just looking to get the sum of all the count(d.name)
values, then this would do that for you:
select sum(countname) as TotalCount
from
(
SELECT Top 10
count(d.name) as countname,
d.name as name
FROM [table 1] as d
group by d.name
order by count(d.name) desc
)a
Upvotes: 4