Come eseguire il loop quando si interroga la data?Ciao a tutti
Sto cercando di interrogare il database utilizzando JMP. Nella mia query, cambio manualmente il valore dateNow dal giorno 1 al 30, quindi eseguo lo script. Ma avevo bisogno di crearne un altro che si ripetesse nel corso dei giorni.
Come posso creare un ciclo che interroghi ogni data a partire da luglio 2023 a gennaio 2024?
dateNow = "01/11/2024";
dt = Open Database( "DSN=databasename;UID=root;PWD=admin;APP=JMP;DATABASE=dbSample").
"select distinct name, age, sex, address
from db1
where date between '" || dateNow || " 00:00:00 AM' and '" || dateNow || " 11:59:59 PM'",
"raw" );
Automazione e scriptingRi: Come eseguire il loop quando si interroga la data?Ecco la mia soluzione.
Names Default To Here( 1 );
For( theDate = Informat( "01/07/2023", "d/m/y" ), theDate <= Informat( "31/1/2024", "d/m/y" ),
theDate = theDate + In Days( 1 ),
// Create the dateNow variable from the index variable "theDate"
dateNow = Format( theDate, "d/m/y" );
dt = Open Database( "DSN=databasename;UID=root;PWD=admin;APP=JMP;DATABASE=dbSample").
"select distinct name, age, sex, address
from db1
where date between '" || dateNow || " 00:00:00 AM' and '" || dateNow || " 11:59:59 PM'",
"raw" );
);
Ri: Come eseguire il loop quando si interroga la data?A seconda del tuo SQL potresti essere in grado di detrarre l'indice dalla data (potrebbe richiedere il troncamento/modifica un po' della tua query)
Names Default To Here(1);
start_date = 01Jul2023;
end_date = 31Jan2024;
datecount = Date Difference(start_date, end_date, "Day");
Show(As Date(start_date + In Days(datecount)));
sql_template = "\[select sysdate - ^i^ startdate, sysdate - ^i - 1^ enddate from dual]\";
For(i = 1, i <= datecount, i++,
cur_sql = Eval Insert(sql_template);
Show(cur_sql);
);
Inoltre molto probabilmente vorrai concatenare i risultati della query all'interno della query e aggiungere qualche semplice registro di stampa può essere utile (di solito stampo almeno: indice/data corrente su cui viene eseguita la query, quante righe hai restituito e quanto tempo ha impiegato la query ).