Easy Way of Querying Multiple Tables

  • I have 8 tables that contain a 'Total' field, what I want to do is the following:

    1. Query the first table and read the Total value into a variable
    2. Query the second table and read the Total value, if the value is less than the first table then replace the value in the variable with this amount
    3. Repeat step 2 for each of the remaining tables

    The idea is that the value returned by the stored procedure will be the lowest 'Total' value of the 8 tables.

  • Either loop through all 8 tables, as you suggest, or do a:

    select  min(x.total)

    from

         (

           select total from table1 where...

           union

           select total from table2 where...

           union

           select total from table3 where...

           ....

          ) x

    /Kenneth

  • Thanks Kenneth

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply