Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL question #2
Message
De
06/08/2009 06:39:01
 
 
À
06/08/2009 05:36:04
Lutz Scheffler (En ligne)
Lutz Scheffler Software Ingenieurbüro
Dresden, Allemagne
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Titre:
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Database:
Visual FoxPro
Divers
Thread ID:
01416409
Message ID:
01416411
Vues:
75
This message has been marked as a message which has helped to the initial question of the thread.
>Gregory,
>
>if I change to
>
>	create cursor Schnaps ;
>	(	c1	c(1), ;
>		c2	c(1),; 
>		c3	c(1),;
>		iOrder	I ;
>	)
>	
>	insert into Schnaps values ('A', 'a', '1',1)
>	insert into Schnaps values ('A', 'b', '2',2)
>	insert into Schnaps values ('A', 'b', '3',3)
>	insert into Schnaps values ('A', 'b', '3',4)
>	insert into Schnaps values ('B', 'c', '4',5)
>	insert into Schnaps values ('B', 'd', '4',6)
>
>
>just to have an order (the import has some relevance in its order, but SQL wil not work with recordnumbers)
>
>What I need now is to delete any record with multiple definitions of c3 except the first one.
>
>Result should be:
>
>('A', 'a', '1',1)
>('A', 'b', '2',2)
>('A', 'b', '3',3)
>('B', 'c', '4',5)
>
>
>Agnes



Agnes,

You just changed the rules
This is not another (distinct) definition of C3 = 3, but another one that is just the same
>	insert into Schnaps values ('A', 'b', '3',3)
>	insert into Schnaps values ('A', 'b', '3',4)
	create cursor Schnaps ;
	(	c1	c(1), ;
		c2	c(1),; 
		c3	c(1),;
		iOrder	I ;
	)
	
	insert into Schnaps values ('A', 'a', '1',1)
	insert into Schnaps values ('A', 'b', '2',2)
	
	insert into Schnaps values ('A', 'b', '3',3)
	insert into Schnaps values ('A', 'b', '3',4) &&
	
	insert into Schnaps values ('B', 'c', '4',5)
	insert into Schnaps values ('B', 'd', '4',6) && 
	insert into Schnaps values ('B', 'd', '4',7) && 
I have added iOrder 7. && are the records that have to be deleted
	select S.* ;
		from Schnaps S, ;
			( ;
				select c3 ;
					from Schnaps ;
					group by  1 ;
					having ( count( *) > 1) ;  && You just changed the rules
			)  AA ;
		where	( S.c3 == AA.c3 ) ;
		into cursor duplicates ;
		order by S.c3, S.iOrder
Now add a recno for the self join
	&& add rec no
	select duplicates.*, ;
			recno() as rec ;
		from duplicates ;
		into cursor duplicates_recno;
		order by c3, iOrder
	 
delete all except first
	delete from Schnaps ;
		where (  ;
				iOrder in ;
					( ;
						select X1.Iorder ;
						from duplicates_recno X1 ;
							left join duplicates_recno X2 on ( X1.c3 == X2.c3 ) ;
											and	( X1.rec -1 == X2.rec ) ;
						where ( X2.c3 is not null ) ;
					) ;
				);
And voila
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform