Two tables A and B, each with a field, update time A.MODIFIED_TM and B.modified_tm,a table as the main table, update time is not empty, but the B table update time may be empty, now to take a, B two table time the newest one, b.modified

Source: Internet
Author: User
Tags null null

Problem Description:

Two Tables A and B, each with a field, update time A.MODIFIED_TM and B.modified_tm,a table as the main table, update time is not empty, but the B table update time may be empty, now to take a, B two table time the newest one, b.modified_ If the TM is empty, take A.modified_tm, for example:

Table A
ID Modified_tm
1 2013/3/10 18:07:12
2 2013/4/10 18:07:12
3 2013/5/10 18:07:12


Table B
ID Modified_tm
1 2014/3/11 18:07:12
NULL NULL
3 null

Results

1 2014/3/11 18:07:12
2 2013/4/10 18:07:12
3 2013/5/10 18:07:12

Left connection


Way One:

SELECT a.ID as ID,
DECODE (B.MODIFIED_TM,
Null
A.MODIFIED_TM,
Greatest (A.modified_tm, B.modified_tm)) as Modified_tm
From A
Left JOIN B
on a.id = b.ID;


Way two:

SELECT a.ID as ID,
Case
When B.modified_tm are not NULL and then
Greatest (A.modified_tm, B.MODIFIED_TM)
ELSE
A.modified_tm
END as Modified_tm
From A
Left JOIN B

on a.id = b.ID;


Way three:

SELECT a.ID,
Greatest (NVL (B.modified_tm, DATE ' 1900-01-01 '),
NVL (A.modified_tm, DATE ' 1900-01-01 '))
From A, B
WHERE a.id = b.id (+)
ORDER by a.ID;








Two tables A and B, each with a field, update time A.MODIFIED_TM and B.modified_tm,a table as the main table, update time is not empty, but the B table update time may be empty, now to take a, B two table time the newest one, b.modified

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.