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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:21:15+00:00 2026-06-03T15:21:15+00:00

I have a data table that contains data which is technically a tree structure,

  • 0

I have a data table that contains data which is technically a tree structure, but the tree is defined by a code and the length of this code.

The Commodity Code table consists of a code and a description:

For example:

Code    Description
------  -------------
0101    Live Animals
01011   Horses
010110  Purebred
010190  Other

The level of an item is calculated by counting the codes beneath it. The code beneath it must be contained in the current code. If that makes any sense.

So in the above example:

0101   is level 0 (nothing is contained in it)
01011  is level 1 (0101 is contained in it)
010110 is level 2 (0101 and 01011 is contained in it)
010190 is level 1 (only 0101 is contained in it)

Is there a way to get these levels in SQL? I am using DB2.

EDIT:
Both Nikola and Gordon’s solutions work well, although I think Nikola’s is slightly quicker! Thank you guys!

Had to make a few edits to account for DB2:

select
    t1.code, count(t2.code)
from commoditycode t1 
left join commoditycode t2
on substr(t1.code, 1, length(t1.code) - 1) like concat(t2.code, '%')
group by t1.code
  • 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-03T15:21:17+00:00Added an answer on June 3, 2026 at 3:21 pm

    A join to itself on code minus last character will find all parents on the right side. Counting them will get the level of item:

    declare @test table (code varchar(10), name varchar(100))
    
    insert into @test values ('0101', 'Live Animals')
    insert into @test values ('01011', 'Horses')
    insert into @test values ('010110', 'Purebred')
    insert into @test values ('010190', 'Other')
    
    select t1.code, t1.name, count (t2.code) + 1 [level]
      from @test t1
      left join @test t2
        on substring (t1.code, 1, len (t1.code) - 1) like t2.code + '%'
     group by t1.code, t1.name
    
    
    code    name            level
    01011   Horses          2
    0101    Live Animals    1
    010190  Other           2
    010110  Purebred        3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains some data given below. It uses a tree
I have a database structure that has a Person table which contains fields such
I have a table that contains some statistic data which is collected per hour.
I have a table that contains temporary data which uses a column to store
I have table that I insert data with following query (from c# code): INSERT
I have a table that contains procedure codes among other data (let's call it
I have a table that contains 4 varying types of data and depending on
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I have table which contains data along with created on date. I want to
I have a large data table that contains a checkbox for each row. I

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.