I am trying to get the total number of visit for customers and get the last visited date and 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
Result
#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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.