I have two tables as shown below:
**tblComments**
Commentid
addeddate
Postid
**tblPosts**
Postid
AddedBy
AddedDate
Title
I want to find the total comments for each post. How to do?
I have two tables as shown below:
**tblComments**
Commentid
addeddate
Postid
**tblPosts**
Postid
AddedBy
AddedDate
Title
I want to find the total comments for each post. How to do?
You need to join the tables to find posts with no comment to generate zero
select
count(c.postid), P.postid --edit
from
tblPosts P
LEFT JOIN
tblComments C On P.postid = C.postid
group by
P.postid
To produce zero for post without comments, T-SQL:
select
P.postid, count(c.postid) -- should not use COUNT(*) for LEFT JOINs
from
tblPosts P
LEFT JOIN
tblComments C On P.postid = C.postid
group by
P.postid
For Postgresql, get the cardinality of rows:
select
P.postid, count(c.*) -- should not use COUNT(*) for LEFT JOINs
from
tblPosts P
LEFT JOIN
tblComments C On P.postid = C.postid
group by
P.postid
Related to this: http://www.ienablemuch.com/2010/04/debunking-myth-that-countdracula-is.html
This is why COUNT(*) should not be used on LEFT JOINs:
create table posts
(
post_id int identity(1,1) not null primary key,
post varchar(max)
);
create table comments
(
post_id int not null references posts(post_id),
comment_id int identity(1,1) not null primary key,
comment varchar(max)
);
insert into posts(post) values('hello');
insert into posts(post) values('oh hai');
insert into comments(post_id,comment) values(SCOPE_IDENTITY(), 1);
-- don't
select p.post_id, COUNT(*) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id
-- do
select p.post_id, COUNT(c.post_id) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id
Output:
post_id comment_count
----------- -------------
1 1
2 1
post_id comment_count
----------- -------------
1 0
2 1
Query-style is more appealing on Postgresql, come to think of it, what we are really counting is the cardinality of the set, not the column(s):
-- looks better on Postgresql, COUNT(c.*)
select p.post_id, COUNT(c.*) as comment_count from
posts p
left join comments c on c.post_id = p.post_id
group by p.post_id
select count(*), postid from tblPosts group by postid
should do the trick I would have thought
EDIT: Edited based on your comment below to get all posts including those with 0 comments. Try it out. I think this should work as you expect
SELECT p.Postid, p.Title, ISNULL(X.count,0)
FROM tblPosts p
LEFT OUTER JOIN
(SELECT postid, count(commentid) as Total
FROM tblComments
GROUP BY Postid) AS X
ON p.Postid = X.Postid
select case(when count(commentid)>0 then count(commentid) else 0 end), postid from tblPosts group by postid