子查询获取父项addby字段

时间:2012-01-26 03:11:30

标签: c# asp.net sql-server sql-server-2008

我试图在我的表中创建一个名为Owner的字段,你在其中选择AddedBy字段,其中parentID等于PostID,到目前为止它只打印出第一个字段,第二个字段始终为null。我在查询上做一个子查询。我试图获取父AddedBy字段

SELECT Level, Sequence, PostID, AddedBy, Title, ParentID, Path_String,
   CASE WHEN ParentID IS NULL THEN
       AddedBy 
   ELSE
       (SELECT AddedBy FROM cte o WHERE o.PostID = ParentID)
   END AS Owner
FROM cte order by Sequence

我试图计算与联接中由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; 

我收到以下错误:

Msg 8120, Level 16, State 1, Procedure sproc_GetPostsByThread, Line 34
Column 'cte.Level' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.

PostID,ParentID,AddedBy,Title,Path_String: - PostID是IdentityColumn Path_String的格式为1 /,1/1 /,1/1/2,ParentID是整数

    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

应该是这个

Level       Sequence                                     PostID      AddedBy      Title     ParentID    Path_String     Owner     Count column 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

1 个答案:

答案 0 :(得分:2)

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;