我试图计算与联接中由ParentID联接的PostID相关的所有帖子的计数,但是我得到一个错误,所以当我按照所有的字段进行分组时,我仍然得到错误: - 错误在< / p>
SELECT s.Level, s.Sequence, s.PostID, s.AddedBy,
s.Title, s.ParentID, s.Path_String,
Owner = COALESCE(o.AddedBy, s.AddedBy), COUNT(r.ParentID)
FROM cte AS s
LEFT OUTER JOIN cte AS o
ON s.ParentID = o.PostID
RIGHT join cte AS r
on s.PostID = r.ParentID
ORDER BY s.Sequence;
PostID,ParentID,AddedBy,Title,Path_String: - PostID是IdentityColumn Path_String的格式为1 /,1/1 /,1/1/2,ParentID是整数
这是我的第一个结果的代码:
SELECT s.Level, s.Sequence, s.PostID, s.AddedBy,
s.Title, s.ParentID, s.Path_String,
Owner = COALESCE(o.AddedBy, s.AddedBy), COUNT(r.ParentID)
FROM cte AS s
LEFT OUTER JOIN cte AS o
ON s.ParentID = o.PostID
join cte AS r
on s.PostID = r.ParentID
GROUP BY s.Level, s.Sequence, s.PostID, s.AddedBy, s.Title, s.ParentID, s.Path_String, COALESCE (o.AddedBy, s.AddedBy)
ORDER BY s.Sequence;
Level Sequence PostID AddedBy Title ParentID Path_String Owner Count
1 00000003 3 kirkdm test NULL 3/ kirkdm 1
2 0000000300000005 5 MikeDM re: test 3 3/5/ kirkdm 2
3 000000030000000500000008 8 Joelene re: test 5 3/5/8/ MikeDM 2
3 000000030000000500000009 9 kirkdm re: test 5 3/5/9/ MikeDM 1
4 00000003000000050000000900000010 10 Crushanin re: test 9 3/5/9/10/ kirkdm 1
应该是带有回复列的列 这是我的第二个结果的代码我想得到子计数的第二个结果:
SELECT s.Level, s.Sequence, s.PostID, s.AddedBy,
s.Title, s.ParentID, s.Path_String,
Owner = COALESCE(o.AddedBy, s.AddedBy)
FROM cte AS s
LEFT OUTER JOIN cte AS o
ON s.ParentID = o.PostID
ORDER BY s.Sequence;
Level Sequence PostID AddedBy Title ParentID Path_String Owner Count Children of Replies here
1 00000003 3 kirkdm test NULL 3/ kirkdm
2 0000000300000005 5 MikeDM re: test 3 3/5/ kirkdm
3 000000030000000500000008 8 Joelene re: test 5 3/5/8/ MikeDM
4 00000003000000050000000800000014 14 Christian re: test 8 3/5/8/14/ Joelene
4 00000003000000050000000800000015 15 Zeke re: test 8 3/5/8/15/ Joelene
3 000000030000000500000009 9 kirkdm re: test 5 3/5/9/ MikeDM
4 00000003000000050000000900000010 10 Crushanin re: test 9 3/5/9/10/ kirkdm
5 0000000300000005000000090000001000000011 11 Tim re: test 10 3/5/9/10/11/ Crushanin
答案 0 :(得分:-1)
添加group by
子句。
group by s.Level, s.Sequence, s.PostID, s.AddedBy, s.Title, s.ParentID, s.Path_String, COALESCE(o.AddedBy, s.AddedBy)