开发者

How to query the maximum value of two columns in two different tables?

开发者 https://www.devze.com 2023-01-18 12:58 出处:网络
I have a requirement. I have two tables say TableA and TableB. Both having a column called \"rec_id\". My requirement is to get the maximum of the values contained in these two columns using a single

I have a requirement.

I have two tables say TableA and TableB. Both having a column called "rec_id". My requirement is to get the maximum of the values contained in these two columns using a single query.

eg: In TableA, I have "rec_id" values as {1开发者_开发百科,5,6} and in TableB, I have "rec_id" values as {1,4,2}. So after executing the query, I want "6" as the result since 6 is the maximum value from these two columns, out of these two tables.

Thanks in Advance, Anish Kurian


select max(rec_id) from 
(
  (select rec_id from tablea)
 union all
  (select rec_id from tableb)
) combined


select max(rec_id) from 
(
  (select MAX(rec_id) AS rec_id from tablea)
 union
  (select MAX(rec_id) AS rec_id from tableb)
) combined

In comparison to Nathan Feger's answer this would be more performant

0

精彩评论

暂无评论...
验证码 换一张
取 消