查詢日期時如何循環?大家好
我正在嘗試使用 JMP 查詢資料庫。在我的查詢中,我手動將 dateNow 值從第 1 天更改為第 30 天,然後執行腳本。但我需要創建另一個可以在所有日子裡循環的內容。
如何建立一個循環來查詢從 2023 年 7 月到 2024 年 1 月之間的每個日期?
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" );
自動化和腳本編寫Re: 查詢日期時如何循環?這是我的解決方案。
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" );
);
Re: 查詢日期時如何循環?根據您的 sql,您可能能夠從日期中扣除索引(可能需要 trunc /稍微修改您的查詢)
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);
);
另外,您很可能希望在查詢中連接查詢結果,並添加一些簡單的列印日誌記錄可能會有所幫助(我通常至少會列印:正在查詢的當前索引/日期、返回的行數以及查詢花費的時間) 。