我正在尝试获取客户的总访问次数并获取上次访问日期和nextVisited
SELECT
COUNT( VisitTracking.customerID) AS #VISIT,
Customers.title,
Customers.firstName,
Customers.LastName,
Company.companyName,
VisitTracking.DateVisited,
CONVERT(date,VisitTracking.nextVisit)AS nextVisit
FROM VisitTracking
INNER JOIN Customers ON VisitTracking.customerID = Customers.customerID
INNER JOIN Customer_Company ON Customers.customerID = Customer_Company.customerID
INNER JOIN Company ON Customer_Company.companyID = Company.companyID
GROUP BY
Customers.title,
Customers.firstName,
Customers.LastName,
Company.companyName,
VisitTracking.DateVisited,
VisitTracking.nextVisit
结果
#VISIT, title, firstName ,LastName, companyName, DateVisited, nextVisit
1, Mr, Tom, Tom, Jedii design, 2012-11-09, 2012-11-14
2 , Mr, Sam, Tom, compudata, 2012-11-10, 2012-11-14
1, Mr, Mike, Mike, compudata, 2012-11-10, 2012-11-14
1, Mr, Mike, Mike, compudata, 2012-11-16 ,2012-11-23
1, Mr, Ryan, Ryan, compudata, 2012-11-07 ,2012-11-09
答案 0 :(得分:0)
SELECT
COUNT( VisitTracking.customerID) AS #VISIT,
Customers.title,
Customers.firstName,
Customers.LastName,
Company.companyName,
max(VisitTracking.DateVisited) as lastVisit,
max(CONVERT(date,VisitTracking.nextVisit)) AS nextVisit
FROM VisitTracking
INNER JOIN Customers ON VisitTracking.customerID = Customers.customerID
INNER JOIN Customer_Company ON Customers.customerID = Customer_Company.customerID
INNER JOIN Company ON Customer_Company.companyID = Company.companyID
GROUP BY
Customers.title,
Customers.firstName,
Customers.LastName,
Company.companyName