Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Two table in one
Message
 
To
21/04/2007 05:46:49
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01218574
Message ID:
01218596
Views:
14
>>>Yes, sometimes 30.
>>>>
>>>>Can you have more then TWO records in Child table for a parent Id?
>>>>
>>>>BTW there is no need to post the question twice. If you didn't have any answer to your question you could click on your post and UPDATE it.
>>
>>Then what you want this table to looks like?
>
>Like written in the "MessageID:1218575", below "The result".
>(Each child records should be in parent table in a separate field.)
*** Just to define how many distinct names we have in child table
SELECT MAX(Cnt) AS Cnt;
       FROM (SELECT Id, COUNT(DISTINCT cField1) AS Cnt;
                    FROM ChildTable;
                    GROUP BY  Id) Tbl1;
INTO CURSOR crsTest
IF crsTest.Cnt = 0
   MESSAGEBOX([What we must do now?])
   RETURN
ENDIF

*** Creates a cursor where we will try to flat the Child table
lcCreate = [CREATE CURSOR crsChid (Id int]
FOR lnFor = 1 TO crsTest.Cnt
    lcCreate = lcCreate + [, pcField] +TRANSFORM(lnFor)+[ C(3)]
NEXT
lcCreate = lcCreate + [)]
&lcCreate


*** Get ALL distinct names from Child table
SELECT DISTINCT Id, cField1;
       FROM ChildTable;
       ORDER BY Id;
INTO CURSOR crsTest && crsTest is not needed anymore

lnId = 0
lnNextField = 1
SCAN 
   IF Id # m.lnId
      lnNextField = 1
      lnId        = crsTest.Id
      APPEND BLANK IN crsChid
   ENDIF
   lcFldName = [pcField]+TRANSFORM(lnNextField)
   REPLACE &lcFldName WITH crsTest.cField1 IN crsChid
   lnNextField = lnNextField + 1
ENDSCAN

*** OK We Flated the cursor so the final result:

SELECT *;
       FROM ParentTable;
       LEFT JOIN crsChild ON ParentTable.Id = crsChild.Id;
INTO CURSOR crsFinal
Not tested but you got the idea.
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform