Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sortear registros al azar
Message
 
To
28/05/2006 15:19:43
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01125405
Message ID:
01125656
Views:
13
Me confundía partido con un "match". Estuve probando con un par de funciones y otra lógica con la estructura que me enviaste. Espero que te sirva el siguiente ejemplo:
*[ cursor donde se guardaran los matchs entre dos anillos
CREATE CURSOR match ;
 (anillo1 N(5), anillo2 N(5))
*[ cursor temporal para evitar hacer llamadas a un función Random
SELECT Anillos.idanillo, Anillos.idpartido as idPartAnillo, Anillos.pesogallo,;
  Anillos.resultado, Partidos.idpartido, Partidos.grupo,;
    .F. as sorteado ;
 FROM ;
     anillos ;
    INNER JOIN partidos ;
   ON  Anillos.idpartido = Partidos.idpartido ;
INTO CURSOR cur_Auxiliar READWRITE

lContinuoSorteando = .T.
DO WHILE lContinuoSorteando
	STORE 0  TO nPartido, nGrupo, nPeso, nAnillo1, nAnillo2
	
	*[ Comienzo a generar un match entre dos rivales
	nAnillo1 = BuscarRival1()
	IF nAnillo1 = -1
		lContinuoSorteando = .F.
	ELSE
		*[ Busco un rival para el primer anillo
		nAnillo2 = BuscarRival2()
		IF nAnillo2 = -1
			lContinuoSorteando = .F.
		ELSE
			*[ genero un match...
			INSERT INTO match (anillo1, anillo2) VALUES (nAnillo1, nAnillo2)
			*[ ...y lo quito para futuras busquedas
			UPDATE cur_Auxiliar SET sorteado = .T. WHERE idAnillo = nAnillo1
			UPDATE cur_Auxiliar SET sorteado = .T. WHERE idAnillo = nAnillo2
		ENDIF
	ENDIF
ENDDO

*[ muestro todos los matchs
SELECT match
BROWSE NOEDIT TITLE "Todos los matchs"

*[ muestro los anillos que no cumplieron la condición para participar
SELECT cur_Auxiliar
BROWSE FOR !sorteado NOEDIT TITLE "Anillos que no participan"

CLOSE TABLES ALL

*[ Busco cualquier anillo que no haya sido sorteado
FUNCTION BuscarRival1()
	LOCAL lnRet

	SELECT TOP 1 * FROM cur_Auxiliar ;
	  WHERE NOT sorteado ORDER BY idAnillo ;
	    INTO CURSOR cur_Rival
	IF RECCOUNT("cur_Rival") > 0    
		lnRet    = cur_Rival.idAnillo
		nPartido = cur_Auxiliar.idPartido
		nGrupo   = cur_Auxiliar.grupo
		nPeso    = cur_Rival.pesogallo
	ELSE 
		*[ no hay mas anillos que cumplan la condición... 
		*[ ...no continuo sorteando
		lnRet = -1
	ENDIF
	USE IN cur_Rival
	RETURN lnRet
ENDFUNC

*[ busco un anillo con condiciones respecto al rival1
FUNCTION BuscarRival2()
	LOCAL lnRet

	SELECT TOP 1 * FROM cur_Auxiliar ;
	  WHERE NOT sorteado AND ;
	       cur_Auxiliar.idPartAnillo != nPartido AND ;
	       cur_Auxiliar.grupo != nGrupo AND ;
	       ABS(cur_Auxiliar.pesogallo - nPeso) < (60) AND ;
	       cur_Auxiliar.idAnillo != nAnillo1 ;
      ORDER BY cur_Auxiliar.idAnillo ;	       
	    INTO CURSOR cur_Rival
	IF RECCOUNT("cur_Rival") > 0    
		lnRet    = cur_Rival.idAnillo
		nPartido = cur_Auxiliar.idPartido
		nGrupo   = cur_Auxiliar.grupo
		nPeso    = cur_Rival.pesogallo
	ELSE 
		*[ no hay mas anillos que cumplan la condición... 
		*[ ...no continuo sorteando
		lnRet = -1
	ENDIF
	USE IN cur_Rival
	RETURN lnRet
ENDFUNC
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform