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 8349687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:05:06+00:00 2026-06-09T08:05:06+00:00

I am dealing with some hierarchical data in the following form: Level Parent PrimaryKey

  • 0

I am dealing with some hierarchical data in the following form:

Level      Parent    PrimaryKey    LevelDepth    RevenuePct
Total       NULL     2786f8161           0           100
US          Total    33f254b0f           1           60
UK          Total    462adbba            1           25
Asia        Total    5322678b3           1           15
Mobile       US      75b72bdf1           2           10
Laptop       US      813784df5           2           10
PC           US      9550f97c            2           15
Consulting   US      a44ae3ef8           2           25
Mobile       UK      ace663d07           2           10
Laptop       UK      b373e61c            2           8
PC           UK      ca590ef44           2           7
Mobile      Asia     d136f267e           2           15

and I want it to be displayed in following form:

Breakup                      Revenue [%]
Total                           100
    US                           60
            Mobile               10
            Laptop               10
            PC                   15
            Consulting           25
    UK                           25
            Mobile               10
            Laptop                8
            PC                    7
    Asia                         15
            Mobile               15

The actual problem has 6-7 level of nesting.

I am relatively new to the field and am trying to use CTE but am having issue with join condition since the child entries repeat in different parents (i.e. I have mobile category for US, UK etc…).

  • 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-09T08:05:08+00:00Added an answer on June 9, 2026 at 8:05 am

    Here is one way to do that. Path column is used for sorting – you should probably instead of \ concatenate fixed-width level to produce path. Query works by recursively calling cte part until no row satisfies join condition between first part of cte (expressed as cte in second part after union all) and table1.

    ; with cte as (
      select level, parent, revenuepct, leveldepth, cast (level as varchar(1000)) Path
        from table1
       where parent is null
      union all
      select a.level, a.parent, a.revenuepct, a.leveldepth, cast (path + '\' + a.level as varchar(1000))
        from table1 a
       inner join cte
          on a.parent = cte.level
    )
    -- Simple indentation
    select space(leveldepth * 4) + level as Breakup, 
           revenuepct as [revenue %]
      from cte
     order by path
    -- Max recursive calls, 0 = unlimited
    option (maxrecursion 10)
    

    Here is Sql Fiddle with example.

    And here is a link to article on recursive cte

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

Sidebar

Related Questions

I am dealing with some auto-generated XSLT code. It contains the following: string(string(.)) number(string(.))
Background: In some PHP code dealing with a 3rd party web-service, the following code
I'm dealing with some very non-uniform data and I'm using Ruby regular expressions to
I'm often dealing with some interfaces between two systems with data import or data
I'm dealing with some legacy code that stores its data in a proprietary string
I'm dealing with some code at work that includes an expression of the form
I'm a fledgling DBA dealing with some medical data. While I've not yet gotten
I'm dealing with some pre-ANSI C syntax. See I have the following function call
I am developing a Python package for dealing with some scientific data. There are
I am dealing with a folder hierarchy like the following: c:/users/rox/halogen/iodine/(some .txt files) c:/users/rox/halogen/chlorine/(some

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.