<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using Max Date to Query in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222716#M44439</link>
    <description>&lt;P&gt;Using my previous example, if you continue with:&lt;/P&gt;
&lt;P&gt;val = Column( 1 )[1];&lt;/P&gt;
&lt;P&gt;show(val);&lt;/P&gt;
&lt;P&gt;Show( As Date( val ) );&lt;/P&gt;
&lt;P&gt;sql = eval insert("select * where purchase_date &amp;gt; ^As Date(val)^");&lt;/P&gt;
&lt;P&gt;show(sql);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the log shows:&lt;/P&gt;
&lt;P&gt;val = 3376598400;&lt;BR /&gt;As Date(val) = 31Dec2010;&lt;BR /&gt;sql = "select * where purchase_date &amp;gt; 31Dec2010";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so you could then run the resulting sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 22 Aug 2019 19:09:04 GMT</pubDate>
    <dc:creator>briancorcoran</dc:creator>
    <dc:date>2019-08-22T19:09:04Z</dc:date>
    <item>
      <title>Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222200#M44337</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a file that is updated weekly based on the Max Date in the date column. Each week I open the file, look at the Max Date currently and then query data that is after the Max Date. Would it be possible to just pull the Max Date out and have it inserted into the query that I am using?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently my file is updated to 8/15/2019 and I would like to query data that is &amp;gt; 8/15/2019 to update the file.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 13:19:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222200#M44337</guid>
      <dc:creator>Sully</dc:creator>
      <dc:date>2019-08-19T13:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222331#M44354</link>
      <description>&lt;P&gt;Without knowing the specifics, it seems like you should be able to keep this all within one query.&amp;nbsp; However, you can always use Query Builder to do a Max on the table in question with the dates, and then extract that value to substitute in another piece of SQL Script.&amp;nbsp; An example of the script from Query Builder working on the movie database, Rentals table where the date column has the date of rental, the script emitted would be like:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;New SQL Query(
	Connection( "ODBC:DSN=SQLServer SQBTest;UID=sa;PWD=%_PWD_%;APP=JMP;WSID=machine;DATABASE=SQBTest;" ),
	QueryName( "g6_Rentals" ),
	Select(
		Column(
			"OrderDate",
			"t1",
			Alias( "Maximum-OrderDate" ),
			SavedJMPName( "OrderDate" ),
			Aggregation( "Maximum" ),
			JMP Name( "Maximum-OrderDate", 0 ),
			Numeric Format( "m/d/y", "-1", "NO", "" )
		)
	),
	From( Table( "g6_Rentals", Schema( "SQBTest" ), Alias( "t1" ) ) )
) &amp;lt;&amp;lt; Run Foreground();

val = Column( 1 )[1];

Show( As Date( val ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here was are just doing an Aggregate of Maximum on the date column.&amp;nbsp; The resulting table has one row and column, and we take the value out of that and show it to the log to make sure it is correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Brian Corcoran&lt;/P&gt;
&lt;P&gt;JMP Development&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 11:51:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222331#M44354</guid>
      <dc:creator>briancorcoran</dc:creator>
      <dc:date>2019-08-20T11:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222715#M44438</link>
      <description>&lt;P&gt;Would you be able to show how you would insert the Max Date into the next SQL from the first table?&lt;/P&gt;&lt;P&gt;The table I am using has a column labeled as Purchase_Date which is what I would do the maximum on in order to query new data from the database. An example of the query is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;dbc = &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;create database connection&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;(&lt;/FONT&gt;&lt;FONT color="#800080" face="Consolas" size="2"&gt;"DSN=PROD; UID="&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;||IDC||&lt;/FONT&gt;&lt;FONT color="#800080" face="Consolas" size="2"&gt;" ;PWD= "&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;||PASSS||&lt;/FONT&gt;&lt;FONT color="#800080" face="Consolas" size="2"&gt;" ;MODE=SHARE;DBALIAS=PROD"&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;data_sql = &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;evalinsert&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;(&lt;/FONT&gt;&lt;FONT color="#800080" face="Consolas" size="2"&gt;"&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Select CUST_NM, PURCHASE_AMT, PURCHASE_DATE, PURCHASE_TM, ORDER_NUM, PRODUCT_NM, PRODUCT_TYP, COUNTRY_CD, APPROVE_DECLINED&lt;/P&gt;&lt;P&gt;from PURCHASE_DATA&lt;/P&gt;&lt;P&gt;where PURCHASE_DATE &amp;gt; '2019-08-16'&lt;/P&gt;&lt;P&gt;and PRODUCT_TYP = 'Y'&lt;/P&gt;&lt;P&gt;and COUNTRY_CD in ('0840')&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800080" face="Consolas" size="2"&gt;and APPROVE_DECLINED ='A'"&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;data_dt = &lt;/FONT&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;execute sql&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt; (dbc, data_sql); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000dd" face="Consolas" size="2"&gt;close database connection&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt; (dbc); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 18:09:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222715#M44438</guid>
      <dc:creator>Sully</dc:creator>
      <dc:date>2019-08-22T18:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222716#M44439</link>
      <description>&lt;P&gt;Using my previous example, if you continue with:&lt;/P&gt;
&lt;P&gt;val = Column( 1 )[1];&lt;/P&gt;
&lt;P&gt;show(val);&lt;/P&gt;
&lt;P&gt;Show( As Date( val ) );&lt;/P&gt;
&lt;P&gt;sql = eval insert("select * where purchase_date &amp;gt; ^As Date(val)^");&lt;/P&gt;
&lt;P&gt;show(sql);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the log shows:&lt;/P&gt;
&lt;P&gt;val = 3376598400;&lt;BR /&gt;As Date(val) = 31Dec2010;&lt;BR /&gt;sql = "select * where purchase_date &amp;gt; 31Dec2010";&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so you could then run the resulting sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 19:09:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222716#M44439</guid>
      <dc:creator>briancorcoran</dc:creator>
      <dc:date>2019-08-22T19:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222832#M44459</link>
      <description>&lt;P&gt;Thanks Brian, that works as expected. Is it possible to have the val reformatted in a numeric format (such as 12/31/2010, or 31/12/2010)? Having a character such as Dec in the date doesn't work for my query.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 13:32:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222832#M44459</guid>
      <dc:creator>Sully</dc:creator>
      <dc:date>2019-08-23T13:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222833#M44460</link>
      <description>&lt;P&gt;Modifying my example from before, you could do:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;val = Column( 1 )[1];

dv = As Date( val );
dvf = Format( dv, "m/d/y" );

Show( dvf );

sql = Eval Insert( "select * where purchase_date &amp;gt; ^dvf^" );

Show( sql );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 13:50:13 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/222833#M44460</guid>
      <dc:creator>briancorcoran</dc:creator>
      <dc:date>2019-08-23T13:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/228338#M45301</link>
      <description>&lt;P&gt;Hi Brian,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to add single quotes around the Date (needed to query from a particular DB). Would I just add the single quote directly to the eval?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;sql = Eval Insert( "select * where purchase_date &amp;gt; '^dvf^'" );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 16:12:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/228338#M45301</guid>
      <dc:creator>Sully</dc:creator>
      <dc:date>2019-10-07T16:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/228352#M45305</link>
      <description>&lt;P&gt;Hi Brian,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was actually able to get it running by editing the lines you have already provided. I just added an extra line to the val portion to include the single quotes. Updated val with single quotes below, feel free to critique if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;val = Column( 1 )[1];

dv = As Date( val );

dvf = Format( dv, "yyyy-mm-dd" );

dvfs = ("'" || dvf || "'");

Show( dvfs );

sql = Eval Insert( "select * where purchase_date &amp;gt; ^dvfs^" );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 16:11:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/228352#M45305</guid>
      <dc:creator>Sully</dc:creator>
      <dc:date>2019-10-07T16:11:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/228407#M45314</link>
      <description>&lt;P&gt;If you're using Oracle you might consider using TO_DATE on your date field so that the date format is unambiguous.&amp;nbsp; If you're using SQL Server yyyy-mm-dd is the default format so no conversion is necessary.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dv = today();
dvf = Format( dv, "yyyy-mm-dd" );

sql = Eval Insert( "select * where purchase_date &amp;gt; TO_DATE('^dvf^', 'yyyy-mm-dd')" );
show(sql);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 20:11:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/228407#M45314</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2019-10-07T20:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using Max Date to Query</title>
      <link>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/243293#M48018</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/4550"&gt;@pmroz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks pmroz. Plain and easy code. Very helpful.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 15:54:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Using-Max-Date-to-Query/m-p/243293#M48018</guid>
      <dc:creator>Thomas1</dc:creator>
      <dc:date>2020-01-28T15:54:47Z</dc:date>
    </item>
  </channel>
</rss>

