【问题标题】:Subquery getting the parent addedby field子查询获取父addby字段
【发布时间】:2012-01-26 03:11:29
【问题描述】:

我试图在我的表中创建一个名为 Owner 的字段,您可以在其中选择已添加的字段,其中 parentID 等于 PostID 到目前为止,它只打印出第一个字段,第二个字段始终为空。我对查询进行子查询。我正在尝试获取父 AdditionalBy 字段

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 相关的所有帖子的数量,但我收到错误,所以当我对所有字段进行分组时,我仍然收到错误:- 错误如下

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

【问题讨论】:

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


    【解决方案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;
    

    【讨论】:

    • 非常感谢您的参与。在过去的几天里,我一直在学习很多关于 SQL 的新东西
    • 你能向我解释一下 COALESCE 吗
    • COALESCE 返回第一个非空表达式。因此,左连接将显示所有者作为父行的 AdditionalBy 值(如果存在);如果没有,它将改为使用当前行中的 AdditionalBy 值。有关COALESCE 的更多详细信息,请参阅此联机丛书主题:msdn.microsoft.com/en-us/library/ms190349.aspx
    • 您能帮我计算子查询的数量吗?当我进行计数时出现错误:'cte.Level' 在选择列表中无效,因为它不包含在任一聚合函数中或 GROUP BY 子句。我需要按顺序排列它们
    • 子查询的计数?抱歉,您将需要显示示例数据和所需的结果。我不知道你要数什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2021-12-10
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 2020-08-15
    相关资源
    最近更新 更多