我创建了这个T-Sql查询,为我提供了多少访问客户并按公司分组 我的Pronlem是我的查询不只计算一次访问而不是按公司分组
SELECT COUNT( VisitTracking.customerID) AS #VISIT
, MAX(VisitTracking.visitID) AS visitID
,Customers.title AS Title
,Customers.customerID
,Customers.firstName AS "First Name"
,Customers.LastName AS "Last Name"
,Company.companyName AS "Company Name"
,VisitTracking.DateVisited AS "Date Visited"
,VisitTracking.nextVisit AS "Next Visit"
FROM VisitTracking INNER JOIN
Customers ON VisitTracking.customerID = Customers.customerID INNER JOIN
Company ON VisitTracking.companyID = Company.companyID
GROUP BY VisitTracking.companyID,Customers.customerID, VisitTracking.customerID, Customers.title, Customers.firstName, Customers.LastName, Company.companyName, VisitTracking.DateVisited,VisitTracking.nextVisit
输出
我想要什么
2 29 Mr 2 Mike Mike N/A 2013-01-31
1 28 Mr 2 Mike Mike Compudata 2013-01-31
答案 0 :(得分:0)
问题是因为您使用的是DataVisited字段,您应该删除字段或执行类似的操作。
SELECT COUNT( VisitTracking.customerID) AS #VISIT
, MAX(VisitTracking.visitID) AS visitID
,Customers.title AS Title
,Customers.customerID
,Customers.firstName AS "First Name"
,Customers.LastName AS "Last Name"
,Company.companyName AS "Company Name"
,MAX(VisitTracking.DateVisited) AS "Date Visited"
,VisitTracking.nextVisit AS "Next Visit"
FROM VisitTracking INNER JOIN
Customers ON VisitTracking.customerID = Customers.customerID INNER JOIN
Company ON VisitTracking.companyID = Company.companyID
GROUP BY VisitTracking.companyID,Customers.customerID, VisitTracking.customerID, Customers.title, Customers.firstName, Customers.LastName, Company.companyName,VisitTracking.nextVisit