Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL to normalize the data!
Message
From
21/07/1999 10:42:47
 
 
To
20/07/1999 23:22:11
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00243953
Message ID:
00244146
Views:
25
I think what you have here is a crosstab, not a normalization. The following code would do it, but it's suboptimal and will flat out blow-up with two many coin types:
OPEN DATABASE mydatabase 
USE original IN 0
***You have to have the new table as part of a DB for long fieldnames
CREATE TABLE crosstab (studentID,studname)
USE IN crosstab
SELECT original
SCAN
   cPotentialField="fld"+original.coins  &&Fields can't start wiht numbers
   USE crosstab IN 0
   COPY STRUCTURE EXTENDED TO temp
   USE IN crosstab
   USE temp IN 0
   SELECT temp
   LOCA FOR UPPER(temp.field_name)=UPPER(cPotentialField)
   IF EOF()
      ALTER TABLE crosstab ADD COLUMN &cPotentialField N(8,0)
   ENDIF
   USE IN temp
   USE crosstab IN 0
   SELECT crosstab
   LOCA FOR studentid = original.studentid
   IF EOF()
      INSERT INTO crosstab (studentid,studname) ;
             VALUES (original.studentid,original.studname)
   ENDIF
   REPLACE (cPotentialField) WITH original.qty
   USE IN crosstab
   SELECT original
ENDSCAN
DELETE FILE temp.dbf



>keke.. Actually, it is a simply question from original...
>
>the real case is NOT Coins, but there are about 40 different items...
>He ask for method to auto-convert One Column data into real field name and
>fill in the corresponding data...
>
><HR>
>>STILL bad. NOT normalized. (lol)
>>
>>Coins should be in a child table of Students as follows (by the way, I like the StudName field...could i be a StudName?)
>>
>>Students:  StudentID,StudName
>>StCoins:   StudentID,Coins,Qty
>>
>>You would convert by scanning through the original table, create parent Students records and child StCoins record.
>>
>>>One friend ask me how to convert the non-normalize dbf to standard!
>>>
>>><PRE>
>>>badtable:
>>>
>>>StudentID    StudName    Coins      Qty
>>>-----------------------------------------
>>>PJ00001      May         5_Dollar    10
>>>PJ00001      May         2_Dollar     4
>>>PJ00002      John        10_Dollar    6
>>>PJ00002      John        2_Dollar     5
>>>PJ00002      John        1_Dollar     3
>>>PJ00003      Peter       5_Dollar     7
>>>PJ00003      Peter       1_Dollar    15
>>>PJ00004      Susan       10_Dollar    2
>>>PJ00004      Susan       5_Dollar     1
>>>...
>>>
>>>
>>>GoodTable:
>>>
>>>StudentID    StudName    1_Dollar  2_Dollar  5_Dollar  10_Dollar
>>>-----------------------------------------------------------------
>>>PJ00001      May            0         4         10         0
>>>PJ00002      John           3         5          0         6
>>>PJ00003      Peter         15         0          7         0
>>>PJ00004      Susan          0         0          1         2
>>>........
>>>
>>>
>>>
>>>
>>>For Report, simply use group can do it, but how to change the table
>>>into this structure na!?
>>>
>>>Thanks!
------------------------------------------------
John Koziol, ex-MVP, ex-MS, ex-FoxTeam. Just call me "X"
"When the going gets weird, the weird turn pro" - Hunter Thompson (Gonzo) RIP 2/19/05
Previous
Reply
Map
View

Click here to load this message in the networking platform