- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
How to loop when querying date?
Hi all
I am trying to query the database using JMP. In my query, I manually change the dateNow value from day 1 to 30 then run the script. But I needed to create another one that would loop all through the days.
How can I create a loop that queries every date starting from July 2023 to January 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" );
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to loop when querying date?
Created:
Feb 12, 2024 09:42 PM
| Last Modified: Feb 12, 2024 7:41 PM
(955 views)
| Posted in reply to message from UserID16644 02-12-2024
Here is my solution.
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" );
);
Jim
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to loop when querying date?
Created:
Feb 12, 2024 09:42 PM
| Last Modified: Feb 12, 2024 7:41 PM
(956 views)
| Posted in reply to message from UserID16644 02-12-2024
Here is my solution.
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" );
);
Jim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to loop when querying date?
Depending on your sql you might be able to deduct index from the date (might require trunc / modifying your query a bit)
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);
);
Also you most likely want to concatenate the query results inside the query and adding some simple print logging can be helpful (I usually print at least: current index/date which is being queried, how many rows you got back and how long the query took).
-Jarmo