Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Table join puzzle
Message
 
 
À
28/07/2004 22:23:54
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00929042
Message ID:
00929287
Vues:
9
Hi Steve,

Here's the first draft.
SET TALK OFF
CREATE CURSOR test ( ;
	ItemNum C(5), Status C(1),  Cust C(3),  Sku C(10))
SET TEXTMERGE ON
SET TEXTMERGE TO ("test.txt")     

TEXT TEXTMERGE NOSHOW PRETEXT 7
I0001,1,149,50391, 
I0001,3,116,654781871
I0002,3,116,651571536
I0002,3,203,103911
I0002,3,213,50197133
I0002,1,225,93818
I0003,1,149,50395
I0004,1,149,50398
I0004,1,175,30919199
I0004,1,198,9858833
I0004,3,213,50197168
I0005,1,103,309193910
I0005,3,149,50611
I0005,1,175,31020155
I0005,3,213,50383541
ENDTEXT

SET TEXTMERGE TO
SET TEXTMERGE OFF

APPEND FROM ("test.txt") DELIMITED

GO TOP
DELETE WHILE EMPTY(ItemNum)
*BROWSE LAST NOWAIT


SELECT *, 000000000 AS rn ;
	FROM test ;
	WHERE Status = "3" ;
	ORDER BY ItemNum, Cust, Sku ;
	INTO CURSOR s3 READWRITE

=AssignRn()

SELECT *, 000000000 AS rn ;
	FROM test ;
	WHERE Status = "1" ;
	ORDER BY ItemNum, Cust, Sku ;
	INTO CURSOR s1 READWRITE

=AssignRn()

SELECT 	NVL(s3.ItemNum, s1.ItemNum) AS ItemNum, ;
		s3.Status, s3.Cust, s3.Sku, ;
		s1.rn, ;
		s1.Status, s1.Cust, s1.Sku ;
	FROM s3 ;
		FULL JOIN s1 ON s3.ItemNum = s1.ItemNum ;
			AND s3.rn = s1.rn ;
	INTO CURSOR crsResult
BROWSE LAST NOWAIT
RETURN
*---------------------------

FUNCTION AssignRn
lcItemNum = "?????"
SCAN
	IF ItemNum <> lcItemNum
		lnRn = 0	
		lcItemNum = ItemNum
	ENDIF
	lnRn = lnRn + 1
	REPLACE rn WITH lnRn
ENDSCAN
RETURN
>
>Imagine the following data, in one table:
>
>
>ItemNum   Status   Cust  Sku
>I0001       1      149   50391
>I0001       3      116   654781871
>I0002       3      116   651571536
>I0002       3      203   103911
>I0002       3      213   50197133
>I0002       1      225   93818
>I0003       1      149   50395
>I0004       1      149   50398
>I0004       1      175   30919199
>I0004       1      198   9858833
>I0004       3      213   50197168
>I0005       1      103   309193910
>I0005       3      149   50611
>I0005       1      175   31020155
>I0005       3      213   50383541
>
>
>An item should only appear in the resultset if and only if it has a status of 3. However, there should also be entries in other fields for occurrences of the same item with a status of 1.
>
>The output in the report would look like this:
>
>
>I0001       3  149  50391         1  149  50391
>----------------------------------------------------
>I0002       3  116  651571536     1  225  93818
>            3  203  103911
>            3  213  50197133
>----------------------------------------------------
>I0004       3  213  50197168      1  149  50398
>                                  1  175  30919199
>                                  1  198  9858833
>----------------------------------------------------
>I0005       3  149  50611         1  103  309193910
>            3  213  50383541      1  175  31020155
>
>
>A join between the items (at least the one I attempted) resulted in twice as many entries, like this, for example:
>
>* Unintented results
>I0005       3  149  50611         1  103  309193910
>            3  149  50611         1  175  31020155
>            3  213  50383541      1  103  309193910
>            3  213  50383541      1  175  31020155
>
>
>I figure I can grab all the status = "3" records, with empty fields for the "1" records, into a readwrite cursor. SCAN/ENDSCAN, looking up "1" records along the way, creating new records to add to the resultset when necessary (like I0004 would require, for example). But I'm looking for a better way.
>
>Any ideas?
>
>Hmmm...an idea just hit me. One record per item. A memo field for status3, another memo for status3, scan/endscan two times, stacking the customer/Sku combinations into the memo fields. Still think there's a better way. :)
>
>Thanks for your attention,
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform