binary tree

  • Hello,

    I have data and i would like to know how i could put it in SQL and making some calculation for example: 1+1+1+3, or 1+2+5+5 and so on? Any ideas?

     

    1

    1 2

    1 4 5

    2 3 4 5

  • You could create a table (b_tree) to store your numeric data in a single column.  Then assign additional columns based on common attributes of the numbers.

    column1: your numbers

    column2: applies to condition 1?  0 or 1 (assign 1 to 1, 1, 1, 3)

    column3: is on the diagonal?  0 or 1 (assign 1 to 1, 2, 5, 5)

    Then your queries would look like:

    select sum(column1) from b_tree where column2=1; (1+1+1+3);

    select sum(column1) from b_tree where column3=1; (1+2+5+5);

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Steve Collins wrote:

    You could create a table (b_tree) to store your numeric data in a single column.  Then assign additional columns based on common attributes of the numbers.

    column1: your numbers

    column2: applies to condition 1?  0 or 1 (assign 1 to 1, 1, 1, 3)

    column3: is on the diagonal?  0 or 1 (assign 1 to 1, 2, 5, 5)

    Then your queries would look like:

    select sum(column1) from b_tree where column2=1; (1+1+1+3);

    select sum(column1) from b_tree where column3=1; (1+2+5+5);

     

    got to disagree on this one - that relies on you having an infinite number of columns (column1,column2....column1024)

    maybe just put them in a good old fashioned key value pair

    create table mytable(parented int, val int)

    select sum(val) from mytable where parentid=1

    if you want to sum based on levels in a tree (ie level 3 - 10) then look at microsofts documentation on hierarchyid data types

    shouldn't be much more than select sum(val) from mytable where isdescendantof(@Hid) and level between 3 and 10

    MVDBA

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply