Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:03:18+00:00 2026-05-28T18:03:18+00:00

Im trying to create a field called Owner in my table where You select

  • 0

Im trying to create a field called Owner in my table where You select the AddedBy field where parentID is equal to the PostID so far it only prints out the first field and the second is always null. Im doing a subquery on a query. Im trying to get the parent AddedBy field

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

Im trying to get a count of all posts that related to the PostID joinded by ParentID in a join but im getting an error so when i do a group by of all the Fields i still get the error:- error is below

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; 

i get the following error:

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 is IdentityColumn Path_String is in this format 1/, 1/1/, 1/1/2 and ParentID is an integer

    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

Should be this

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:03:19+00:00Added an answer on May 28, 2026 at 6:03 pm
    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a Crystal Reports formula field (to calculate the percentage change
I'm trying to create a new div each time a text field contained in
I am trying to create a form in Rails 2.2.2 that populates a field
I am trying to create a unique constraint on two fields in a table.
I'm trying to create a table with Listview and one of the fields I'm
I'm trying to populate a field called 'identification' using the primary key 'id'. However,
I have a table called posts having an INT field posted and a table
I'm trying to create a function that shows a modal dialog which when called
I'm trying to create a filter in ASP.net, where a user can select a
I am trying to create a custom field type in SharePoint. My custom field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.