Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Array passing in SQL UDF
Message
De
09/01/2000 12:52:05
 
 
À
08/01/2000 20:59:40
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00314970
Message ID:
00315161
Vues:
1561
>>Ed,
>>
>>Just to let you know what I am trying to do:
>>
>>I have a Customer table and a child Purchased table. I want an SQL to determine those Customers who bought
>>SOAP and TOWELS and TOOTHBRUSH.
>>
>>The UDF (obviously not the one show) determines if the pair Cust_id,Item_id exists for each Item in the search list
>>(by using SEEK) and returns .T. or .F. The values of Item_id are stored in an array.
>>
>>Is this a totally off the wall approach and dangerous to boot?
>
>
>select cust_id from customer where cust_id in
>      (select cust_id from purchased
>         where item_id = soap and cust_id in
>             (select cust_id from purchased
>                where item_id = towels  and cust_id in
>                    (select cust_id from purchased where item_id = toothbrush)
>             )
>      )

I'd take a radically different approach:  build a cursor with the items that
must be on the order to qualify, and call that MustHave.  Now try:

nNumItems = RECCOUNT('MustHave')
SELECT cust_id, item_id FROM Purchased ;
  INTO CURSOR CandCusts ;
 WHERE item_id IN (SELECT item_id from MustHave) ;
 GROUP BY cust_id, Item_Id ;
 ORDER BY cust_id, Item_id

SELECT Cust_ID, COUNT(*) AS NumMatches FROM CandCusts ;
  INTO CURSOR TheseCustsBoughtEverything ;
 GROUP BY Cust_ID ;
HAVING NumMatches = nNumItems
The first SELECT pulls order items that are on our list of MustHave items; we want only one record per cust_id, item_id pair, so I've cheated and used a GROUP BY rather than a DISTINCT clause to cull duplicates. We now have a table of all cust_ids that matched some item_id we were looking for.

We want only those cust_ids that have all the MustHave items; we can discover this by counting how many records are in our intermediate candidate file for each cust_id. Any cust_id that has as many records as there are MustHave items represent customers who purchased evrything of interest at least one time.
EMail: EdR@edrauh.com
"See, the sun is going down..."
"No, the horizon is moving up!"
- Firesign Theater


NT and Win2K FAQ .. cWashington WSH/ADSI/WMI site
MS WSH site ........... WSH FAQ Site
Wrox Press .............. Win32 Scripting Journal
eSolutions Services, LLC

The Surgeon General has determined that prolonged exposure to the Windows Script Host may be addictive to laboratory mice and codemonkeys
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform