1

I have two tables.

Table1:
id, date

Table2:
id,date

Both the table contain information about id. Table1 and Table2 can have some extra rows which are not present in another table.

Example:
Table1:
1,15-Jun
2,16-Jun
4,17-Jun

Table2 
1,14-Jun
2,17-Jun
3,18-Jun

I need a summarize result which give minimum date for each row. Expected result:

1,14-Jun
2,16-Jun
3,18-Jun
4,17-Jun
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
  • Please search before posting questions. Answered here: http://stackoverflow.com/questions/7877062/select-max-min-values-from-two-tables – Mitch3a Dec 04 '14 at 13:39
  • A side remark: As you may see from some of the answers, it is not considered a good idea to call a column date. You can use it such as in `select "date" from table1`, but it is recommended not to use reserved words for column names. – Thorsten Kettner Dec 04 '14 at 13:40
  • @ThorstenKettner ya thanks. I used that as example to easily understanding of datatype if required. – Vivek Goel Dec 04 '14 at 13:43

3 Answers3

3
select id, min(date_) from (
    select id, date_ from table1 
    union all
    select id, date_ from table12
) group by id;
Multisync
  • 8,657
  • 1
  • 16
  • 20
1
SELECT id, MIN(date)
FROM (SELECT id, date
      FROM Table1
      UNION
      SELECT id, date
      FROM Table2)
GROUP BY id
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
  with a as(select t.i_id,t.dt_date from t
union 
select b.i_id,b.dt_date from b)
select a.i_id,min(a.dt_date) from a group by a.i_id order by a.i_id;

You can check this link

smn_onrocks
  • 1,282
  • 1
  • 18
  • 33
  • @LalitKumarB How can you claim this is just duplicate answare from your answar you cun't expect that way of doing a same work will different didderent. – smn_onrocks Dec 05 '14 at 10:33