Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 tables that are similar:

Table PROBEVALIDATIONZAXIS
Columns:
timeofentry
jobnumber
zhit
machine

Table PROBEVALIDATIONHOLE
Columns:
timeofentry
jobnumber
zhit
machine

Table PROBEVALIDATIONCORNER
Columns:
timeofentry
jobnumber
zhit
machine

I want to display a distinct jobnumber for a certain machine but I also want to display a date. The problem I am having is that if I include the timeofentry in the select statement, then my select no longer displays a distinct job number because the time of entry is different in each table. How can I get it to display the min date from all 3 columns along with the job number?

If I run the query now (see below), I get the following:

11/14/2016 05:52:10 AM 28810
11/14/2016 05:52:16 AM 28810
11/14/2016 05:52:24 AM 28810
11/14/2016 06:52:10 AM 28811
11/14/2016 06:52:16 AM 28811
11/14/2016 06:52:24 AM 28811


What I want is the result with the minimum date for every job number:
11/14/2016 05:52:10 AM 28810
11/14/2016 06:52:10 AM 28811


I should also add that I originally had the union commands as minus commands, however this only works if the data is in all 3 tables. If it is just in 1 or 2 tables, then I don't get all the results

What I have tried:

SQL
Select timeofentry, JOBNUMBER
FROM PROBEVALIDATIONHOLE
WHERE HOLENUMBER = 1 AND TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
union
SELECT timeofentry, JOBNUMBER
FROM PROBEVALIDATIONZAXIS
WHERE ZHIT = 1 AND TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
union
SELECT timeofentry, JOBNUMBER
FROM PROBEVALIDATIONCORNER
WHERE TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
Posted
Updated 14-Nov-16 5:37am

Did you tried like this
select t.JOBNUMBER, MIN(t.timeofentry) from (
Select timeofentry, JOBNUMBER
FROM PROBEVALIDATIONHOLE
WHERE HOLENUMBER = 1 AND TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
union
SELECT timeofentry, JOBNUMBER
FROM PROBEVALIDATIONZAXIS
WHERE ZHIT = 1 AND TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
union
SELECT timeofentry, JOBNUMBER
FROM PROBEVALIDATIONCORNER
WHERE TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
)t group by t.JOBNUMBER
 
Share this answer
 
Comments
theskiguy 15-Nov-16 6:31am    
Works great! I was trying something similar to that but I wasn't using the Group by. Thanks for the help.
manu_dhobale 15-Nov-16 7:45am    
Thanks..
Have a try with an inline view like the following
SQL
select a.jobnumber, min(a.timeofentry)
from (Select timeofentry, JOBNUMBER
      FROM PROBEVALIDATIONHOLE
      WHERE HOLENUMBER = 1 AND TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
      union
      SELECT timeofentry, JOBNUMBER
      FROM PROBEVALIDATIONZAXIS
      WHERE ZHIT = 1 AND TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500'
      union
      SELECT timeofentry, JOBNUMBER
      FROM PROBEVALIDATIONCORNER
      WHERE TIMEOFENTRY between to_date('11/13/2016','MM/DD/YYYY HH:MI:SS AM') and to_date('11/14/2016 11:59:59 PM','MM/DD/YYYY HH:MI:SS AM') and machine = 'NC-500') a
group by a.jobnumber
 
Share this answer
 
Comments
theskiguy 15-Nov-16 6:31am    
Your solution works but it is basically the same as the first solution. Thanks for you help.
Wendelius 15-Nov-16 6:37am    
Yes it seems to be the same. As you can see there's two minutes difference so we seemed to write the solution at the same time.

Glad that you got it working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900