在我的数据库中,我有进行购买的客户,然后这些购买与协助购买的员工相关联。我在下面写了一个查询,它将为我提供一个客户列表,其中包括他们总共购买了多少次、他们的第一次购买和最后一次购买。我还想要与上次购买关联的员工姓名?
Customer
-cstId
-cstName
Purchase
-cstId
-soldDate
-empId
Employee
-FirstName
-LastName
-empId
SELECT customer.cstName, MAX(purchase.soldDate) AS [Last Purchase], MIN(purchase.soldDate) AS [First Purhcase], COUNT(invTruck.invId)
AS [Total Purchases]
FROM customer INNER JOIN
purchase ON customer.cstId = purchase.cstId
INNER JOIN
employee ON purchase.empId = employee.empId
GROUP BY customer.cstName