- Comment threading with score factoring
- (01)"This is a top level comment."
- (02)-"This is a second level comment. A reply to the top level comment above."
- (06)-"This is also a second level comment / another reply to comment 01."
- (07)--"This is a reply to comment 06."
- (03)"This is a different top level comment."
- (05)-"This is a reply to the comment above."
- (08)--"This is a reply to that comment in turn."
- (10)---"This is a deeper comment still."
- (04)"This is one more top level comment."
- (09)-"This is one more reply."
- Comment ID | Parentage
- 01 | (Comment 01 has no parent because it is top level)
- 02 | 01- (Comment 02 was a reply to comment 01)
- 03 |
- 04 |
- 05 | 03-
- 06 | 01-
- 07 | 01-06- (Comment 07 has two ancestors 01 and then 06)
- 08 | 03-05-
- 09 | 04-
- 10 | 03-05-08-
- Comment ID | Parentage
- 01 |
- 02 | 01-
- 03 |
- 04 |
- 05 | 03-
- 06 | 01-
- 07 | 01-06-
- 08 | 03-05-
- 09 | 04-
- 10 | 03-05-08-
- 11 | 03-05-08-10-
- Order by CONCAT(Parentage, ID)
- Comment ID | Parentage | CONCAT(Parentage, ID)
- 01 | | 01-
- 02 | 01- | 01-02-
- 06 | 01- | 01-06-
- 07 | 01-06- | 01-06-07-
- 03 | | 03-
- 05 | 03- | 03-05-
- 08 | 03-05- | 03-05-08-
- 10 | 03-05-08- | 03-05-08-10-
- 11 | 03-05-08-10- | 03-05-08-10-11-
- 04 | | 04-
- 09 | 04- | 04-09-
- (01)"This is a top level comment."
- (02)-"This is a reply to the top level comment."
- (06)-"This is another reply that was posted later than the first."
- (07)--"This is a reply to the second level comment directly above."
- (03)"This is a different top level comment."
- (05)-"This is a reply to the comment above."
- (08)--"This is a reply to the comment above."
- (10)---"This is a deeper comment still."
- (11)----"THIS COMMENT WAS ADDED IN THE EARLIER EXAMPLE."
- (04)"This is one more top level comment."
- (09)-"This is one more reply."
- (01)"This is a top level comment." [6 votes]
- (02)-"This is a reply to the top level comment." [2 votes]
- (06)-"This is another reply that was posted later than the first." [30 votes]
- (07)--"This is a reply to the second level comment directly above." [5 votes]
- (03)"This is a different top level comment." [50 votes]
- (05)-"This is a reply to the comment above." [4 votes]
- (08)--"This is a reply to the comment above." [0 votes]
- (10)---"This is a deeper comment still." [0 votes]
- (11)----"THIS COMMENT WAS ADDED IN THE EARLIER EXAMPLE." [0 votes]
- (04)"This is one more top level comment." [2 votes]
- (09)-"This is one more reply." [0 votes]
- "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."
- 'CommentTree' has more columns than were specified in the column list.
- ParentId | CommentId | Comment | Vote | Level
- NULL | 1 | Text here | 6 | 0
- NULL | 3 | Text here | 50 | 0
- NULL | 4 | Text here | 2 | 0
- 4 | 9 | Text here | 0 | 1
- 3 | 5 | Text here | 4 | 1
- 5 | 8 | Text here | 0 | 2
- 8 | 10 | Text here | 0 | 3
- 10 | 11 | Text here | 0 | 4
- 1 | 2 | Text here | 2 | 1
- 1 | 6 | Text here | 30 | 1
- 6 | 7 | Text here | 5 | 2
- Comment ID | Parent ID | Comment | Vote
- WITH CommentTree (ParentId, CommentId, Comment, Vote)
- AS
- (
- -- Anchor member definition
- SELECT c.ParentId, c.CommentId, c.Comment, c.Vote,
- 0 AS Level
- FROM dbo.Comments AS c
- WHERE ParentId IS NULL
- UNION ALL
- -- Recursive member definition
- SELECT c.ParentId, c.CommentId, c.Comment, c.Vote,
- Level + 1 AS Level
- FROM dbo.Comments AS c
- INNER JOIN CommentTree AS d
- ON c.ParentID = d.CommentID
- Order by C.Vote
- )
- SELECT ParentId, CommentId, Comment, Vote FROM CommentTree