Solved

Get Child Data From Parent Data

Posted on 2009-07-12
13
285 Views
Last Modified: 2012-05-07
Hi,

I have Chart Of Account Table:

1. AccNo
2. Name
3. GroupAcc   --> Refer to AccNo

I want to get all child value by passing parent AccNo.

It could be done using recursive CTE in SQL2005.

How to solve it using SQL 7 syntax?

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834505
some looping t-sql
create table #children ( accno int , lev int )
set nocount on 
declare @lev int
declare @root_accno int 
set @root_accno = 123
set @lev = 0 
insert into #children (accno, lev ) values ( @root_accno, @lev )
while @@rowcount > 0
begin
  set @lev = @lev + 1
  insert into #children (accno, lev) select t.accno, @lev from #children c join yourtable t on t.groupacc = c.accno and c.lev = @lev - 1
end 
select * from #children

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834525
Hi angelIII,

Let me try first.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834558
Please see my code, some change from your code.

Incorrect syntax near the keyword 'SET'.

Why SET @root_accno = '123' ?

Thank you.
CREATE TABLE #children(accno Char, lev int)
SET nocount ON 
    DECLARE @lev int DECLARE @root_accno Char,
SET @root_accno = '123'
SET @lev = 0
        INSERT 
      INTO #children(accno, lev)
VALUES (@root_accno, @lev) 
    WHILE @@rowcount > 0 BEGIN
SET @lev = @lev + 1
        INSERT 
      INTO #children(accno, lev)
              SELECT t .accno, @lev
            FROM #children c LEFT JOIN
                TMGLACCNO AS t ON 
                t .GlobalAcc = c.accno AND 
                c.lev = @lev - 1 END
                    SELECT *
                  FROM #children

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834604
you had the CHAR, in the declare...
CREATE TABLE #children(accno varChar(100), lev int)
SET nocount ON 
 DECLARE @lev int 
DECLARE @root_accno varChar(100)
SET @root_accno = '123'
SET @lev = 0
        INSERT 
      INTO #children(accno, lev)
VALUES (@root_accno, @lev) 
    WHILE @@rowcount > 0 BEGIN
SET @lev = @lev + 1
        INSERT 
      INTO #children(accno, lev)
              SELECT t .accno, @lev
            FROM #children c LEFT JOIN
                TMGLACCNO AS t ON 
                t .GlobalAcc = c.accno AND 
                c.lev = @lev - 1 END
                    SELECT *
                  FROM #children

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834624
Time out expired

SET @root_accno = '101.000.00' is the global account to find.

Thank you.
SET nocount ON 
    DECLARE @lev int DECLARE @root_accno varChar(100)
SET @root_accno = '101.000.00'
SET @lev = 0
        INSERT 
      INTO #children(accno, lev)
VALUES (@root_accno, @lev) 
    WHILE @@rowcount > 0 BEGIN
SET @lev = @lev + 1
        INSERT 
      INTO #children(accno, lev)
              SELECT t .accno, @lev
            FROM #children c LEFT JOIN
                TMGLACCNO AS t ON 
                t .GlobalAcc = c.accno AND 
                c.lev = @lev - 1 END
                    SELECT *
                  FROM #children

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24834628
not LEFT JOIN. JOIN
also, do you have a index on globalAcc on the table?
SET nocount ON 
    DECLARE @lev int DECLARE @root_accno varChar(100)
SET @root_accno = '101.000.00'
SET @lev = 0
        INSERT 
      INTO #children(accno, lev)
VALUES (@root_accno, @lev) 
 
WHILE @@rowcount > 0
BEGIN
  SET @lev = @lev + 1
  INSERT 
    INTO #children(accno, lev)
   SELECT t .accno, @lev
            FROM #children c 
            JOIN  TMGLACCNO AS t ON 
                t .GlobalAcc = c.accno AND 
                c.lev = @lev - 1 
END
                    SELECT *
                  FROM #children

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834709
do you have a index on globalAcc on the table?
No.

Some accounts appear more than once? To be exactly 3 times.

Thank you.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834721
>>do you have a index on globalAcc on the table?
>No.
please create one with globalAcc + Accno on that table. will boost performance of that extraction query


>Some accounts appear more than once? To be exactly 3 times.
could it be that your structure is "incorrect"?

can you provide test data?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834736
Sure.

Thank you.
CHART-OF-ACCOUNT.xls
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834761
based on your test data, I got this, which seems just perfect, no duplicates ...
please double check your data/output.

for example:

101.001.01      1
101.001.02      1
101.001.03      1
101.002.01      1
101.003.01      1
101.003.03      1

is no duplicates, as the middle part changes...
accno           lev
--------------- -----------
101.000.00      0
101.001.01      1
101.001.02      1
101.001.03      1
101.002.01      1
101.003.01      1
101.003.03      1
101.004.01      1
101.005.01      1
101.005.02      1
101.005.03      1
101.005.06      1
101.005.07      1
101.005.08      1
101.005.09      1
101.006.02      1
101.006.03      1
101.007.01      1
101.009.01      1
101.101.01      1

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834772
I see now.

May I executed it more than once.

How to delete the #children before create it?

Thank you.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24834775
>How to delete the #children before create it?
DELETE #children
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 24834794
Hi angelIII,

My problem solved.

Thank you very much for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL trigger 5 49
Select question from MySQL 1 29
Finding Where Clause Value in SQL Views and SP 21 60
TSQL remove duplicates from different columns 14 52
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question