Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Max (orderdate) two tables query and stored proc

Author  Topic 

DareDelSol
Starting Member

1 Post

Posted - 2002-11-04 : 19:00:58
I have two tables "currentorders" and "historyorders". How do I find the MAX (orderdate) of both tables combined (besides the current orders with todays date)? Meaning the greater of the two tables. and what would the syntax of this be to create a stored procedure?

This would tell me when the last order was placed not including the current order where the results will be displayed.

Thanks!


Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2002-11-04 : 19:12:47
Hi

Next time, can you post some DDL (Create table scripts) and maybe even some example data ? Otherwise it's just a guess.

But, at a guess.....



Select orderid, max(orderdate)
From (
select orderid, orderdate from currentorders
UNION ALL
select orderid, orderdate from historyorders
) orders

Where
orderid <> @CurrentOrder and
DateDiff(d, orderdate, GetDate()) <> 0

Group by orderid


Hope that helps. As for stored proc syntax, look up Create Procedure in Books Online

Damian
Go to Top of Page
   

- Advertisement -