I have a query that uses a TOP 1 clause (query2) that I want to join to another query that uses aggregate functions, but I’m not sure how to do it.
I’d like a resulting query that looks something like this:
"select Count(*), Avg(AmtSpent), TOP 1 (Outlet_Code)...."
Please kindly advise. Thanks.
Query 1:
select Count(*), Avg(AmtSpent) from Transact Where CardNo In
(Select CardNo from Card where MemberID = 'Mem003')
Query 2:
select TOP 1 (Outlet_Code) from Transact where CardNo In
(Select CardNo from Card where MemberID = 'Mem003')
group by Outlet_Code
Order by count(Outlet_Code) desc
Something like this, as long as each subquery is only returning one row, which looks to be the case because one of your queries is selecting top 1, and the other has no group by clause. This works in SQL Server 2008.