Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Pentuplicate rows in results
Message
De
30/03/2009 12:34:38
 
 
À
Tous
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Titre:
Pentuplicate rows in results
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Database:
Oracle
Divers
Thread ID:
01392142
Message ID:
01392142
Vues:
108
I'm amending some SQL that I didn't write.
It compares sales of various products with those over the same period one year prev. eg if the user select Nov 2008 it compares with Nov 2007.
It can be a break-down by product alone (showing what was invoiced to each cust who bought it) or by product within customer.
The other criterion is that only products that show sales in both periods
I've got the customer part licked but when doing just by product (where don't care is same cust bought in both periods) I get multiple lines. For one product in particular, for example, 1010104, I get 5 rows for period 1 (Nov 2008) for each. Now that product was sold to 5 custs in Nov 2008, and to 7 in 2007. But these 5 don't intersect any of the 7.

Code below:

AcctID is the cust acc ID, AliasID is the product ID
	IF 	.Include.Value
 *            Must have a sale in both periods
 		m.MacroJoin = "INNER JOIN"
 	ELSE
 		m.MacroJoin = "FULL OUTER JOIN"
 	ENDIF

	If INLIST( m.Version, "1", "2")		&& If by customer													
		lcMacroOn			= "cSales1.AcctiD == cSales2.Acctid AND cSales1.AliasID == cSales2.AliasID "		
		IF .Include.Value					&& Must be sales in both periods for this cust.					
			lcMacroWhere	= "cSales1.Sell > 0 AND cSales1.Qty > 0 " + ;
						 "AND EXISTS ( Select DISTINCT cSales2.AcctID " + ;
								"from   cSales2, cSales1 " + ;
								"where	cSales2.AcctiD == cSales1.Acctid " + ;
								"and 	cSales2.AliasID == cSales1.AliasID " + ;
								"and	        cSales2.Sell	>  0 " + ;
								"and 	cSales2.Qty 	>  0) "		
		Else					&& don't care if in both periods							
			lcMacroWhere 	= ".T."													
		EndIf 												
	Else						&& By product							
		lcMacroOn			= "cSales1.AliasID = cSales2.AliasID "				
		IF .Include.Value		&& Must be sales in both periods	
			lcMacroWhere	= "cSales1.Sell > 0 AND cSales1.Qty > 0 " + ;
							  "AND EXISTS ( Select DISTINCT cSales2.AcctID " + ;
								"from  	cSales2, cSales1 " + ;
							       "where 	cSales2.AliasID	== cSales1.AliasID " + ;
								"and 	cSales2.Sell	>  0 " + ;
								"and 	cSales2.Qty 	>  0 ) " 	
		Else					&& don't care if in both periods			
			lcMacroWhere 	= ".T."	
		EndIf 								
	EndIf 										

 	SELECT DISTINCT NVL(cSales1.Acctid,cSales2.Acctid) as Acctid,;
			NVL(cSales1.AliasId,cSales2.AliasId) as AliasId,;
			NVL(cSales1.Qty,0000000000.000) as Qty1,;
			NVL(cSales1.Sell,0000000000.00) as Sell1,;
			NVL(cSales1.Cost,0000000000.00) as Cost1,;
	 		NVL(cSales2.Qty,0000000000.000) as Qty2,;
			NVL(cSales2.Sell,0000000000.00) as Sell2,;
			NVL(cSales2.Cost,0000000000.00) as Cost2 ;
		FROM cSales1 &MacroJoin cSales2 ;
			ON &lcMacroOn ;
			WHERE &lcMacroWhere ;
		INTO CURSOR cSales12
An example of the output below:
Prod 1010104                 
 NOV
2008   2007
Cust A
115.76 17.34
115.76 143.86
115.76 128.45
115.76 17.98 
115.76 35.96 

Cust B
26.46 17.98
26.46 35.96
26.46 128.45
26.46 143.86

Cust C
18.52 128.45
18.52 17.34
18.52 17.98
18.52 143.86
18.52 35.96 

Cust D
55.57 17.98 
55.57 35.96 
55.57 17.34 
55.57 143.86
55.57 128.45

Cust D
297.68 128.45
297.68 17.98 
297.68 17.34 
297.68 143.86
297.68 35.96 
Totals for product 1010104 … …
                            2569.95 1717.95 
Another report gives sales only of the same product, for Nov 2008:
Cust A
Inv 68089 12Nov08   115.76

Cust B
Inv 66861 3Nov08       26.46

Cust C
Inv 68123 14Nov08     18.52

Cust D
Inv 68164 26Nov08     55.57

Cust E
Inv 68185 18Nov08   198.45
Inv 68185 19Nov08    99.23
Total for stock item 1010104 … 513.99
And for Nov 2007:
Cust F
Inv 36389 12Nov07 96.34
Inv 36389 26Nov07 32.11

Cust G
Inv 36405 6Nov07 17.98

Cust H
Inv 36447 7Nov07 17.98

Cust I
Inv 36451 27Nov07 17.34

Cust J
Inv 36457 1Nov07 17.98
Inv 36457 8Nov07 17.98
Note the erroneous compare output takes the customers from the Nov 2008 report but throws in every permutation of the sales in both periods.
Can anyone pleas help me sort this out. I've been on it for days and it's getting very tedious and I'm under pressure. I've just about thrown at it every idea I have.

'ppreciate it

Terry

PS Sorry I have to go home now but will pick it up tomorrow.
- Whoever said that women are the weaker sex never tried to wrest the bedclothes off one in the middle of the night
- Worry is the interest you pay, in advance, for a loan that you may never need to take out.
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform