Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

max value in multiple tables 1

Status
Not open for further replies.

aliashippysmom

Programmer
Jul 29, 2004
43
US
Hi! I can't seem to figure this out. I have five tables that contain a dateUpdated field. I need to find the maximum value of this field for all five tables. I want to execute a query which returns one value, the maximum date of all the dateUpdated fields.

I've tried things like this:

Code:
select max(dateUpdated) from
(
  select max(dateUpdated) as the_date
   from table1
  UNION
  select max(dateUpdated) as the_date
    from table2
  UNION
select max(dateUpdated) as the_date
   from table3
  UNION
  select max(dateUpdated) as the_date
    from table4
  UNION
  select max(dateUpdated) as the_date
    from table5)

Can anyone see what's wrong with this? It does not work. (I get a syntax error.) Thanks in advance.
 
You need to use the alias name in the outer query.

Code:
select max([!]the_date[/!]) from
(
  select max(dateUpdated) as the_date
   from table1
  UNION
  select max(dateUpdated) as the_date
    from table2
  UNION
select max(dateUpdated) as the_date
   from table3
  UNION
  select max(dateUpdated) as the_date
    from table4
  UNION
  select max(dateUpdated) as the_date
    from table5) As A

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I tried this:
Code:
select max(the_date)  from
(
  select max(dateUpdated) as the_date
   from BusUICont
  UNION
  select max(dateUpdated) as the_date
    from BusUICont
  UNION
  select max(dateUpdated)  as the_date
     from BusUI
  UNION
  select max(dateUpdated) as the_date
     from BusUICall
  UNION
  select max(dateUpdated) as the_date
     from BusUITax 
)	as my_date

I still cannot make this work. I must be missing something.
 
I don't notice anything wrong with the query (except the first 2 unions are coming from the same table [smile]). Are you getting error messages? What is it? Or is this just producing the wrong results?



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
o.k. This worked:
Code:
SELECT     MAX(the_date) AS Expr1
FROM         (SELECT     MAX(dateUpdated) AS the_date
                       FROM          BusUIBen
                       UNION
                       SELECT     MAX(dateUpdated) AS the_date
                       FROM         BusUICont
                       UNION
                       SELECT     MAX(dateUpdated) AS the_date
                       FROM         BusUI
                       UNION
                       SELECT     MAX(dateUpdated) AS the_date
                       FROM         BusUICall
                       UNION
                       SELECT     MAX(dateUpdated) AS the_date
                       FROM         BusUITax) my_date

Seems like it didn't like the as my_date at the end of it all. I was getting a syntax error, but now I no longer get that and I get the correct results. Thanks to everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top