在下表中,我尝试计算在给定日期范围内CustomerID和User组合出现多次的记录总数。
/*
SQLyog Community v12.12 (64 bit)
MySQL - 5.5.40-MariaDB
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
create table `VWAE` (
`CustomerID` mediumint (6),
`User` int (11),
`DemoOrderDate` date
);
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('135068','1210','2015-05-27');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153305','1210','2015-06-03');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('152566','1210','2015-06-04');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('152566','1210','2015-06-05');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153896','1210','2015-06-05');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153041','1210','2015-06-08');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('152566','1210','2015-06-08');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('137405','1210','2015-06-11');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('149813','1210','2015-06-15');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('149813','1210','2015-06-16');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153698','1210','2015-06-17');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153698','1210','2015-06-19');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153698','1210','2015-06-22');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('149813','1210','2015-06-22');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('150919','1210','2015-06-23');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('153698','1210','2015-06-23');
insert into `VWAE` (`CustomerID`, `User`, `DemoOrderDate`) values('149813','1210','2015-06-26');
SELECT count(*)
FROM VWAE
WHERE `User` = 1210
AND DemoOrderDate BETWEEN '2015-06-19' AND '2015-06-26'
GROUP BY CustomerID, `User`
HAVING COUNT(*) > 1
查询返回以下两行,这些行无论如何都是正确的。
CustomerID User DemoOrderDate
149813 1210 2015-06-22
153698 1210 2015-06-19
但是,我在计算返回的数字或记录后,而不是获取实际的行。如何实现这一目标?
仅为记录,上述查询是一个更大查询的子查询,其中我计算各种数据的总数。我已经尝试使用上面的查询来创建派生表t1,然后对它的行执行计数,但在我的实际查询中,WHERE User
= 1210子句实际上是引用了范围之外的别名子查询,导致出现以下错误:
Error Code: 1054
Unknown column 'PRF.user_id' in 'where clause'