<?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>Question Re: SQL Command to select MAX(date) &amp; Max(time) in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773638#M2458951</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wayne,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You'll actually want to contaminate the date and time together to get a true date time value. Not sure what type of database you are using so I can only guess at syntax. But you can try something like this...&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT 
"labor"."order-no", 
"labor"."oper-no",
MAX(CAST("labor"."end-date" + "labor"."end-time" AS DateTime)) AS myDateTime
FROM "E940LIVE"."PUB"."tm-log" "labor"
GROUP BY "labor"."order-no", "labor"."oper-no"
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HTH,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jason&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 24 Mar 2010 07:16:36 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2010-03-24T07:16:36Z</dc:date>
    <item>
      <title>SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaq-p/6773637</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My am having a really hard time getting a sql command to return data with the max(date) &amp;amp; max(time) combination. I think I need a sub query but can not figure it out. I have looked all over the internet but still don't understand it. Below is my command which returns the max date &amp;amp; max time but they are not from the same row of data. Please help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT  "labor"."order-no", "labor"."oper-no", MAX("labor"."end-date") AS myDate, Max("labor"."end-time") AS myTime&lt;/P&gt;&lt;P&gt;FROM   "E940LIVE"."PUB"."tm-log" "labor"&lt;/P&gt;&lt;P&gt; WHERE  "labor"."order-no"='73153-bc' AND "labor"."company"='01'&lt;/P&gt;&lt;P&gt;GROUP BY  "labor"."order-no", "labor"."oper-no"&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Mar 2010 23:36:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaq-p/6773637</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-23T23:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773638#M2458951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wayne,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You'll actually want to contaminate the date and time together to get a true date time value. Not sure what type of database you are using so I can only guess at syntax. But you can try something like this...&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT 
"labor"."order-no", 
"labor"."oper-no",
MAX(CAST("labor"."end-date" + "labor"."end-time" AS DateTime)) AS myDateTime
FROM "E940LIVE"."PUB"."tm-log" "labor"
GROUP BY "labor"."order-no", "labor"."oper-no"
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HTH,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jason&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Mar 2010 07:16:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773638#M2458951</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-24T07:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773639#M2458952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Jason,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My data base is Progress OpenEdge. I tried your proposal before making my post and had problems with that as well. Part of my problem lies in that the time field is a integer representing the number of seconds, 9:09:01AM is 32,941. I was able to get the timestamp combination with this code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT  "labor"."order-no", "labor"."oper-no", TO_TIMESTAMP(({fn CONVERT("labor"."end-date" ,SQL_VARCHAR)}) + ' ' +&lt;/P&gt;&lt;P&gt;(RTRIM(({fn CONVERT({fn CONVERT("labor"."end-time"/3600,SQL_INTEGER)},SQL_VARCHAR)}&lt;EM&gt;':'),' ')&lt;/EM&gt;RTRIM(LTRIM(({fn CONVERT(({fn CONVERT(("labor"."end-time"/3600 - {fn CONVERT("labor"."end-time"/3600,SQL_INTEGER)})*60,SQL_INTEGER)}),SQL_VARCHAR)}+':'),' '),' ') +&lt;/P&gt;&lt;P&gt;LTRIM(({fn CONVERT(   ({fn CONVERT(       (("labor"."end-time"/3600 - {fn CONVERT("labor"."end-time"/3600,SQL_INTEGER)} )&lt;STRONG&gt;60 -    {fn CONVERT(         ("labor"."end-time"/3600 - {fn CONVERT("labor"."end-time"/3600,SQL_INTEGER)} )&lt;/STRONG&gt;60  ,SQL_INTEGER)}) *60     ,SQL_INTEGER)}) ,SQL_VARCHAR)}), ' ')  ) ) AS myTime&lt;/P&gt;&lt;P&gt;FROM   "E940LIVE"."PUB"."tm-log" "labor"&lt;/P&gt;&lt;P&gt; WHERE  "labor"."order-no"='73153-bc' AND "labor"."company"='01'&lt;/P&gt;&lt;P&gt;GROUP BY  "labor"."order-no", "labor"."oper-no", myTime&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But when I put the max function around TO_TIMESTAMP like this MAX(TO_TIMSTAMP **** ) AS myTime I got error:&lt;/P&gt;&lt;P&gt;Aggregate function not allowed here (7642) [Database Vendor Code: -20023]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Wayne&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Mar 2010 12:17:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773639#M2458952</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-24T12:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773640#M2458953</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wayne,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not familiar w Progress OpenEdge or it's available functions, but if the time field is simply an INT showing the number of seconds past midnight... You should be able to simply add the seconds to date field using a DateAdd function.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DateAdd(ss,"labor"."end-time", "labor"."end-date") AS myDateTime
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You'll need check the syntax for OpenEdge... &lt;/P&gt;&lt;P&gt;This also assumes that the end-date field is an actual DateTime field type. If not, you may need to cast or convert the end-date field as a data type that will allow for the time to be included.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HTH,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jason&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Mar 2010 13:34:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773640#M2458953</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-24T13:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773641#M2458954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Progress does not support the DATEADD function. Waht if I forget the number is time and look at it just as a number. Here is my data and what I have tried (again I don't understand the multiple select concept yet).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;oper-no   end-date    end-time&lt;/P&gt;&lt;P&gt;  20      2/2/2010     41,975&lt;/P&gt;&lt;P&gt;  30      2/3/2010     45,906&lt;/P&gt;&lt;P&gt;  30      2/16/2010    32,941&lt;/P&gt;&lt;P&gt;  40      2/4/2010     46,099&lt;/P&gt;&lt;P&gt;  40      2/4/2010     50,227&lt;/P&gt;&lt;P&gt;  40      2/4/2010     59,466&lt;/P&gt;&lt;P&gt;  40      2/4/2010     62,024&lt;/P&gt;&lt;P&gt;  40      2/16/2010    43,838&lt;/P&gt;&lt;P&gt;  60      2/17/2010    32,679&lt;/P&gt;&lt;P&gt;  90      2/25/2010    35,270&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="--------------------------------------" /&gt;&lt;P&gt;SQL Command&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT a."oper-no", a."end-time", a."end-date"&lt;/P&gt;&lt;P&gt;FROM   "E940LIVE"."PUB"."tm-log" a, (SELECT "end-time", max("end-date") AS max_date FROM "E940LIVE"."PUB"."tm-log" WHERE  "order-no"='73153-bc' AND "company"='01' GROUP BY "end-date", "end-time") b&lt;/P&gt;&lt;P&gt; WHERE  a."end-time" = b."end-time" AND a."end-date" = b.max_date AND a."order-no"='73153-bc' AND a."company"='01'&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="----------------------------------" /&gt;&lt;P&gt;Result&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;oper-no   end-date     end-time&lt;/P&gt;&lt;P&gt;  20      2/2/2010      41,975&lt;/P&gt;&lt;P&gt;  30      2/3/2010      45,906&lt;/P&gt;&lt;P&gt;  40      2/4/2010      50,227&lt;/P&gt;&lt;P&gt;  40      2/4/2010      46,099&lt;/P&gt;&lt;P&gt;  40      2/4/2010      59,466&lt;/P&gt;&lt;P&gt;  40      2/4/2010      62,024&lt;/P&gt;&lt;P&gt;  30      2/16/2010     32,941&lt;/P&gt;&lt;P&gt;  40      2/16/2010     43,838&lt;/P&gt;&lt;P&gt;  60      2/17/2010     32,679&lt;/P&gt;&lt;P&gt;  90      2/25/2010     35,270&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="-------------------------------------" /&gt;&lt;P&gt;Desired Result&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;oper-no   end-date    end-time&lt;/P&gt;&lt;P&gt;  20      2/2/2010     41,975&lt;/P&gt;&lt;P&gt;  30      2/16/2010    32,941&lt;/P&gt;&lt;P&gt;  40      2/16/2010    43,838&lt;/P&gt;&lt;P&gt;  60      2/17/2010    32,679&lt;/P&gt;&lt;P&gt;  90      2/25/2010    35,270&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for any and all help!&lt;/P&gt;&lt;P&gt;Wayne&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Mar 2010 18:03:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773641#M2458954</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-24T18:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773642#M2458955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;See what this does for you...&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT
a."oper-no",
a."end-date",
MAX(a."end-time") AS 
FROM "tm-log" AS a
INNER JOIN (
	SELECT 
	"oper-no",
	MAX("end-date") AS MaxEndDate,
	FROM "tm-log"
	WHERE a."order-no"='73153-bc' AND a."company"='01'
	GROUP BY "oper-no") AS d 
ON a."oper-no" = d."oper-no" AND a."end-date" = d.MaxEndDate
GROUP BY a."oper-no", a."end-date"
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The subquery will filter the the records down to only those on the "last date for each oper-no.&lt;/P&gt;&lt;P&gt;Using an INNER join will force that filtering onto the outer query which can then be further filtered to find the largest number value in the end-time, for each oper-no.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't have any similar data to test on but it should work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jason&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Mar 2010 19:56:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773642#M2458955</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-25T19:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773643#M2458956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your code did not work as submitted, it gave me a syntax error (probably a progress thing). I finally got some support from progress and got code that works. It still does not create a finial datestamp, but is returning the fields from the correct rows.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SELECT "order-no", "oper-no","end-date","end-time"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;FROM&lt;/P&gt;&lt;P&gt;    "PUB"."tm-log"&lt;/P&gt;&lt;P&gt;WHERE&lt;/P&gt;&lt;P&gt;    "end-date" IN (SELECT&lt;/P&gt;&lt;P&gt;                       MAX("end-date")&lt;/P&gt;&lt;P&gt;                   FROM&lt;/P&gt;&lt;P&gt;                       "PUB"."tm-log"&lt;/P&gt;&lt;P&gt;                   WHERE  "order-no"='73153-bc' AND "company"='01'&lt;/P&gt;&lt;P&gt;                   GROUP BY&lt;/P&gt;&lt;P&gt;                       "oper-no")&lt;/P&gt;&lt;P&gt;    AND&lt;/P&gt;&lt;P&gt;    "end-time" IN (SELECT&lt;/P&gt;&lt;P&gt;                       MAX("end-time")&lt;/P&gt;&lt;P&gt;                   FROM "PUB"."tm-log"&lt;/P&gt;&lt;P&gt;                   WHERE  "order-no"='73153-bc' AND "company"='01'&lt;/P&gt;&lt;P&gt;                   GROUP BY&lt;/P&gt;&lt;P&gt;                   "oper-no",&lt;/P&gt;&lt;P&gt;                   "end-date")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for all your help&lt;/P&gt;&lt;P&gt;Wayne&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Mar 2010 14:48:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773643#M2458956</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-26T14:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Command to select MAX(date) &amp; Max(time)</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773644#M2458957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;yea, I see where you would catch an error in the code I supplied... I copied the WHERE clause directly from your previous post and forgot to remove the "a." aliases. Oops. Sorry.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Glad you got it working. As long as you are getting the correct rows back from the database, you can easily convert the date &amp;amp; time to a single DateTime stamp... CR DOES have the DateAdd function.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DateAdd("s", {Command.end-time}, {Command.end-date})
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jason&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Mar 2010 15:39:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-command-to-select-max-date-max-time/qaa-p/6773644#M2458957</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-03-26T15:39:08Z</dc:date>
    </item>
  </channel>
</rss>

