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

  • Home
  • SEARCH
  • 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 8291855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:16:34+00:00 2026-06-08T13:16:34+00:00

Update: I asked the question because the result does not look like how it

  • 0

Update:
I asked the question because the result does not look like how it is executed… which is explained here:
http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx

Well I have another question…
If I do not need the same parents and the Level, is it possible to return one parent only once using the CTE (not the select after it) or maybe some other sql?

===========================
Desired result is like:

......
    54          4           
    **** the above is anchor member, the numbers are correct  
    4           1          
    1           0           
    2           1           
    36          35          
    35          8           
    8           1           
    54          12          
    12          1           
    11          1           
    3           1           

===========================

I am using a recursive query to find out all the parents from a Hierarchy table, for items from Items table; I thought the result should be by Level, but it is not… I know I can sort it using order by, I just think the output itself should be ordered by Level because the Recursive Memeber is run by Level, right?

            WITH Result(ItemID, ParentID, Level)
            AS
            (
                --get the anchor member from tbItems
                SELECT itemID, itemParentID, 0 AS Level
                FROM   tbItems  WHERE
                    approved = 0 
                UNION ALL
                --recursive member from tbHierarchy
                SELECT h.hierarchyItemID, h.parentItemID, Level + 1
                FROM tbHierarchy AS h
                INNER JOIN
                    Result AS r
                        ON
                        h.hierarchyItemID = r.ParentID          

            )
            SELECT *
            FROM Result

the Result is:

    ItemID      ParentID    Level
    ----------- ----------- -----------
    7           3           0
    11          2           0
    18          11          0
    19          11          0
    21          54          0
    31          2           0
    33          36          0
    34          36          0
    35          36          0
    36          36          0
    38          2           0
    39          2           0
    40          2           0
    54          4           0
    **** the above is anchor member, the numbers are correct  
    4           1           1
    1           0           2
    2           1           1
    1           0           2
    2           1           1
    1           0           2
    2           1           1
    1           0           2
    36          35          1
    35          8           2
    8           1           3
    1           0           4
    36          35          1
    35          8           2
    8           1           3
    1           0           4
    36          35          1
    35          8           2
    8           1           3
    1           0           4
    36          35          1
    35          8           2
    8           1           3
    1           0           4
    2           1           1
    1           0           2
    54          12          1
    12          1           2
    1           0           3
    11          1           1
    1           0           2
    11          1           1
    1           0           2
    2           1           1
    1           0           2
    3           1           1
    1           0           2
  • 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-06-08T13:16:35+00:00Added an answer on June 8, 2026 at 1:16 pm

    The data in a database are not ordered. If you do not put an order by, you cannot be sure of the order of the output.

    Never think the database will do things the way you think, 90% of the time, it’s wrong. The objective of database is to understand your query and find the fastest way to find the solution, and it’s often not the way you think.

    Depending of your editor, but sometimes you can ask it to explain the plan used to find the solution, it might give you some explanation of how the result is obtained.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a question here: Extending javascript literal object which was solved because I
So I asked does https://stackoverflow.com/questions/11144014/does-mysql-update-add-a-row-if-it-doesnt-exist and I was told that I'm using columns incorrectly.
I asked a similar question like this yesterday but after waiting for ever I
This is a follow-up question to a different question I asked not too long
I first asked this question, which I figured out how the EDT works and
Question 828421 asked similar question, but received only one real answer (update rubygems) and
I just asked this question . Which lead me to a new question :)
Variations of this question have been asked before, but it seems like the issue
This is an offshoot question that's related to another I asked here . I'm
I had asked a question on how to implement real time updates in ASP.NET

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.