SQL
DECLARE @topCustomers Table(
Email nvarchar(500),
MoneySpent decimal(18,2)
)
insert into @topCustomers
select top 2
o.CustomerEmail,
sum(p.Price * ol.Quantity) as 'Total Money Spent'
from
orders o
join OrderLines ol on ol.OrderId = o.OrderId
join Products p on p.ProductId = ol.ProductId
group by o.CustomerEmail
order by sum(p.Price * ol.Quantity) desc
select
tc.Email,
FORMAT(o.OrderId,'000000') as 'Order Number',
o.OrderDate,
p.Name as 'Product Name',
ol.Quantity as 'Quantity Ordered'
from @topCustomers tc
join orders o on tc.Email = o.CustomerEmail
join OrderLines ol on ol.OrderId = o.OrderId
join Products p on p.ProductId = ol.ProductId
order by
tc.MoneySpent desc,
o.OrderDate asc