Question : Sql Server 05: How to return multiple columns in a group by max query?

I have the following sql which returns the last sales date per customer. I need to also know what the amount and product was associated with that sale.  Notice that I have two tables where i have to get the info from, an archive table and a non-archive table.   The archive table has an extra filter on it (account closed).  I am using SQL server 2005.

select
max(Q1.Date) "LastSalesDate",
Q1.CustomerID
from
(
select amount, product, date
from
Sales
where Amount > 0 and CompletedSales = 'yes'

union

select amount, product, date
from
SalesArchive
where Amount > 0 and CompletedSales = 'yes' and accountclosed = 'no'


) Q1
group by Q1.CustomerID


Answer : Sql Server 05: How to return multiple columns in a group by max query?

or
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
;with CTE as (
	select customerID, amount, product, date
	from Sales
	where Amount > 0 and CompletedSales = 'yes'
	union
	select CustomerID, amount, product, date
	from SalesArchive
	where Amount > 0 and CompletedSales = 'yes' and accountclosed = 'no'
), CTE2 as (
	select *, row_number() over (partition by CustomerID order by [date] desc) rn from CTE
)
select * from CTE2
where rn = 1
Random Solutions  
 
programming4us programming4us