Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sum of Count

Author  Topic 

Kotti
Posting Yak Master

129 Posts

Posted - 2008-12-24 : 04:30:39
Hi Friends,

I used this query

select (count(c.PostId)) as TotalComments from ez_post as p inner join ez_comment as c
ON p.PostId=c.PostId
where p.BlogId=1 group by c.PostId

I got result like this
2
1
3

But I need sum of that rows(2+1+3).may i know how to get this?

raky
Aged Yak Warrior

767 Posts

Posted - 2008-12-24 : 04:41:26
hi try this

select count(c.PostId) as TotalComments into #temp from ez_post as p inner join ez_comment as c
ON p.PostId=c.PostId
where p.BlogId=1 group by c.PostId

select sum(TotalComments) as TotalComments from #temp
Go to Top of Page

Kotti
Posting Yak Master

129 Posts

Posted - 2008-12-24 : 06:07:10
What is #temp?

My result is not static ,it will change .Any posibility to do in another way?
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-12-24 : 06:10:58
quote:
Originally posted by Kotti

What is #temp?

My result is not static ,it will change .Any posibility to do in another way?



#temp is a temporary table. I think no problem with it. U can use it.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-12-24 : 07:28:33
No need of temporary table

select sum(TotalComments) as TotalComments from
(
select (count(c.PostId)) as TotalComments from ez_post as p inner join ez_comment as c
ON p.PostId=c.PostId
where p.BlogId=1 group by c.PostId
) as t


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-12-24 : 07:58:55
Can't you directly use like this?

select sum(c.PostId) as TotalComments from ez_post as p inner join ez_comment as c
ON p.PostId=c.PostId
where p.BlogId=1
Go to Top of Page
   

- Advertisement -