Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Self Join Table (Recursive Hierarchy)

Self Join Table (Recursive Hierarchy)

Scheduled Pinned Locked Moved Database
databasetutorialsql-serversysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    i Have Self join Table in Sql server Database(accounting database) this is table columns(Accounts Table) AccountID ParentAccountID AccountNo AccountName and i have another table (Accounts Balance Table) AccountBalanceID AccountID FirstBalanceCredit FirstBalanceDebit TotalCredit TotalDebit in accountbalance i add balance for accounts in last level which is not parent for any other accounts so my question is how to get sum of debit or credit in any level for example if i have accountx which is parent so it doesn't have any rows in accountbalance because it's not last level i need to get total debit for that account which is the total accounts for all accounts under him which may not be the last level thanks for any help

    md_refay

    N 1 Reply Last reply
    0
    • L Lost User

      i Have Self join Table in Sql server Database(accounting database) this is table columns(Accounts Table) AccountID ParentAccountID AccountNo AccountName and i have another table (Accounts Balance Table) AccountBalanceID AccountID FirstBalanceCredit FirstBalanceDebit TotalCredit TotalDebit in accountbalance i add balance for accounts in last level which is not parent for any other accounts so my question is how to get sum of debit or credit in any level for example if i have accountx which is parent so it doesn't have any rows in accountbalance because it's not last level i need to get total debit for that account which is the total accounts for all accounts under him which may not be the last level thanks for any help

      md_refay

      N Offline
      N Offline
      NavnathKale
      wrote on last edited by
      #2

      with Account(AccountID)
      As
      (
      select AccountID from Accounts A where AccountID = #AcountNumber#
      union all
      select A.AccountID from Accounts A, Account B
      where A.ParentAccountID = B.AccountID
      )

      SELECT sum(totaldebit) TotalDebit FROM Account, AccountsBalance where
      Account.AccountID = AccountsBalance.AccountID

      replace #AcountNumber# with your account number (probably use variable/parameter). Hope this will help you :cool:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups