This is MYSQL/ SQL running against data on Oracle server. I have a SQL table that stores data as follows:
{IMPORTID Created IMPORTREQUESTID STARTDATE ENDDATE
1156 04/21/2017 (12 AM) 63833 4/23/2017 18:18 4/23/2017 18:19
1156 04/21/2017 (12 AM) 63833 4/23/2017 18:18 4/23/2017 18:18
1156 04/21/2017 (12 AM) 63832 4/23/2017 17:56 4/23/2017 17:57
1156 04/21/2017 (12 AM) 63832 4/23/2017 17:56 4/23/2017 17:57
1156 04/21/2017 (12 AM) 63832 4/23/2017 17:56 4/23/2017 17:57}
I want the Query to go through the table and find the HIGHEST or MAX IMPORTREQUESTID and max ENDDATE for any given IMPORTID. And then calculate the difference between CREATED and max(ENDDATE) for any given IMPORTID, once it finds the row containing maximum values. Again, I only want the max IMPORTREQUESTID and ENDDATE tied to a given IMPORTID. (An IMPORTID will have several rows with different IMPORTREQUESTIDS)
So, for the given data above my query should return only 1 row as follows:
{IMPORTID Created IMPORTREQUESTID STARTDATE ENDDATE DIFF
1156 04/21/2017 12 AM 63833 4/23/2017 18:18 4/23/2017 18:19 **6:19**}
So, in this case it found that IMPORTREQUESTID 63833 was the highest value and THEN enddate of 18:19 was highest time. It then picked that row and calculated difference between created and ENDDATE.
Here is query I have so far:
SELECT ImportID
,A.created
,IMPORTSTATUS
,IMPORTCANONICALTYPE
, to_char(StartDate,'MM/DD/YYYY HH24:MI') StartDate
, to_char(EndDate,'MM/DD/YYYY HH24:MI') EndDate
, ((A.created-A.Enddate)*60*24) as MinDiff
, ImportRequestID
FROM (SELECT DISTINCT importrequestpartdone.ImportID
,importrequestpartdone.IMPORTCANONICALTYPE
,importrequestpartdone.IMPORTSTATUS
,importrequestpartdone.ImportRequestID
, importrequestpartdone.StartDate
, importrequestpartdone.EndDate
,import.created
From import,baywatchnosnap.importrequestpartdone@db1
where contextid=10114
and import.id=importrequestpartdone.importid
and importrequestpartdone.importstatus='2I') A
This returns all the rows. I only want the max values as indicated above. So, each IMPORTID, should only be returning 1 row.