0

我创建了这个 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
4

1 回答 1

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
于 2013-02-07T16:27:43.417 回答