Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Please advice how to best report normalized tables
Message
De
07/02/2006 09:18:50
 
 
À
06/02/2006 22:20:24
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
01094184
Message ID:
01094249
Vues:
8
Hi Alejandro,
I don't know about how VFP 9 can or cannot help you, because I'm on 6.0 SP5 myself. I have a block of code here that shows the 'idea' of dynamically building a field list & joins based on details being in one table & header info being in another. This code snipit contains plenty of room for optimization, error checking, and added flexibility to work across different situations. It's hopefully a good starting point for you to use as an example.
create cursor headerCursor ( myID I, myLabel C(20) )
INSERT INTO headerCursor (myID, myLabel) VALUES ( 1, "First Record" )
INSERT INTO headerCursor (myID, myLabel) VALUES ( 2, "Second Record" )
INSERT INTO headerCursor (myID, myLabel) VALUES ( 3, "Third Record" )
INSERT INTO headerCursor (myID, myLabel) VALUES ( 4, "Fourth Record" )
INSERT INTO headerCursor (myID, myLabel) VALUES ( 5, "Fifth Record" )

create cursor detailCursor( myID I, dtlLabel C(20), dtlValue C(20) )
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 1, "COLOR", "RED")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 1, "BIRTHDATE", "04/13/1976")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 1, "SPEED", "123")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 1, "VISIBLE", ".F.")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 1, "ORIGIN", "SPACE")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 2, "VALUE", "0")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 2, "TEXT", "0")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 2, "VISIBLE", ".T.")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 3, "MOVABLE", ".F.")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 3, "COLOR", "GREEN")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 4, "COLOR", "BLACK")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 4, "VISIBLE", ".T.")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 4, "VALUE", "10")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 4, "TEXT", "TEN")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 4, "MOVABLE", ".T.")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "SPEED", "500")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "BIRTHDATE", "08/08/1980")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "COLOR", "WHITE")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "VALUE", "37")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "ORIGIN", "BOONDOCKS")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "VISIBLE", ".T.")
INSERT INTO detailCursor( myID, dtlLabel, dtlValue ) VALUES ( 5, "MOVABLE", ".F.")

select distinct dtlLabel from detailCursor into cursor attList nofilter order by dtlLabel

cFieldList = ""
cJoinStatement = ""

select attList
go top in attList
scan
	cFieldList = cFieldList + iif(!empty(cFieldList), ",", "") + ;
		"iif(isNull(" + alltrim(attList.dtlLabel) + "alias.dtlValue), SPACE(20)," + ;
		alltrim(attList.dtlLabel) + "alias.dtlValue) as " + alltrim(attList.dtlLabel)
	cJoinStatement = cJoinStatement + ;
		" LEFT OUTER JOIN detailCursor " + alltrim(attList.dtlLabel) + "alias ON headerCursor.myID = " + ;
		alltrim(attList.dtlLabel) + "alias.myID and " + alltrim(attList.dtlLabel) + "alias.dtlLabel = '" + alltrim(attList.dtlLabel) + "' "
endscan

select headerCursor.*, &cFieldList ;
	from headerCursor &cJoinStatement
>We have some transaction tables that are not properly normalized, with transaction records containing some detail fields right in it. I know this is against good practice but 13 years ago it seemed like a good trade-off to gain speed, plus we had few detail types back then. With this structure it is easy to derive cursors that have all fields of interest in one record and reports can show all fields in one line very naturally and speedily.
>
>If we fully normalize those tables and the transaction detail fields are stored in several records of a child table how do I print reports with all those fields in one line? Sounds like a cross-tab problem, but I am not very impressed with the speed of crosstab. It is also not as intuitive to develop. What is the best way to approach this problem? By now we are having too many new detail types and the previous method is getting unwieldy.
>
>Is this a place where the new VFP9 capability of "Subqueries in a SELECT List" help? Actually I don't know how one would use "Subqueries in a SELECT List" at all.
>
>Thank you very much.
>
>Alex
Paul A. Busbey
Victoria Insurance
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform