Programmer
Programmer

Reputation: 325

Get latest record by date

I have table called AUDIT_LOG_1 as AL1 having date as an attribute of DATE data type. I have another table AUDIT_LOG_2 as AL2 having date as an attribute of DATE data type.

I want to compare the date attributes of both the table in a query like AL1.date = AL2.date The comparision is not only limited to date but also to time mean The comparision should happen with the date and time both like 08-09-2014 16:29:42 not only on date Lets say in table AUDIT_LOG_1 table has records as below: 08-09-2014 08-09-2014 08-10-2014 consider that 1st records is insert when time is 16:29:42 and second is inserted when time is 16:31:42

and in AUDIT_LOG_2 table has one row as below: 08-09-2014 and this record is also when time is 16:29:42

so I need to get one record based on date and time match in both the table

Now since its a date type it will display day in table but actually it also has timestamp hidden. I need a Oracle query for this.

Upvotes: 0

Views: 226

Answers (1)

Theo Müller
Theo Müller

Reputation: 45

If you are going to join the to logs via the date, you can issue:

SELECT * FROM AL1 INNER JOIN AL2 ON (al1.date = al2.date)

If you are going to find the latest record (as the title of your question suggest), you can do it this way:

SELECT * FROM AL1 ORDER BY date DESC

or, if you need the results from both tables

( SELECT * FROM AL1
  UNION
  SELECT * FROM AL2
) ORDER BY date DESC

Upvotes: 1

Related Questions