0

visitmysql表:

+---------+-------------+------------+--------+---------------------+
| visitId | visitSiteId | homeSiteId | userId |       time          |
+---------+-------------+------------+--------+---------------------+
|   1     |      1      |     1      |  1001  | 2013-09-01 11:50:12 |
|   2     |      1      |     1      |  1001  | 2013-09-03 12:50:12 |
|   3     |      2      |     1      |  1001  | 2013-09-04 13:50:12 |
|   4     |      2      |     1      |  1001  | 2013-09-07 11:00:00 |
|   5     |      2      |     1      |  1001  | 2013-09-09 12:32:12 |
|   6     |      2      |     1      |  1001  | 2013-09-13 15:48:12 |
|   7     |      2      |     2      |  1002  | 2013-09-01 11:00:12 |
|   8     |      2      |     2      |  1002  | 2013-09-02 12:50:12 |
|   9     |      2      |     2      |  1002  | 2013-09-05 13:50:12 |
|   10    |      1      |     2      |  1002  | 2013-09-06 11:50:12 |
|   11    |      1      |     2      |  1002  | 2013-09-07 12:50:12 |
|   12    |      2      |     2      |  1002  | 2013-09-15 13:50:12 |
+---------+-------------+------------+--------+---------------------+

记录访问日期时间、访问的站点和用户主站点

我想找到如何从上述数据中输出 3 个报告。

  1. 返回所有非主页访问次数 >= 主页访问次数的用户;任何日期
  2. 返回任何具有非家庭站点访问计数 >= 输入的最小整数的用户,例如非家庭访问次数 >= 10
  3. 1 和 2 的日期范围即时间 >= '2013-09-01 11:50:12' AND Time <= '2013-09-13 15:48:12'

我对 MySQL 非常陌生,并且在使用子查询语法时遇到了困难,因此将不胜感激: The Queries below will return the counts of Home Visits and Non Home Visits but I can get my head around Subquery for combined result

SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site <> v.HomeSite GROUP BY v.user_id;
SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site = v.HomeSite GROUP BY v.user_id;
SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site <> v.HomeSite AND v.Time >= '2013-09-01 11:50:12' AND v.Time <= '2013-09-13 15:48:12' GROUP BY v.user_id;
SELECT COUNT(v.user_id), v.user_id FROM visit v WHERE v.Site = v.HomeSite AND v.Time >= '2013-09-01 11:50:12' AND v.Time <= '2013-09-13 15:48:12' GROUP BY v.user_id;

这是帮助测试的 MySQL 创建表和插入数据:

 CREATE TABLE `dbName`.`visit`(`visitId` INT(11) NOT NULL AUTO_INCREMENT, `visitSiteId` INT(11), `homeSiteId` INT(11), `userId` INT(11), `time` DATETIME, PRIMARY KEY (`visitId`));

INSERT INTO `dbName`.`visit`(`visitSiteId`,`homeSiteId`,`userId`,`time`) VALUES
  (1,1,1001,'2013-09-01 11:50:12'),
  (1,1,1001,'2013-09-03 12:50:12'),
  (2,1,1001,'2013-09-04 13:50:12'),
  (2,1,1001,'2013-09-07 11:00:00'),
  (2,1,1001,'2013-09-09 12:32:12'),
  (2,1,1001,'2013-09-13 15:48:12'),
  (2,1,1002,'2013-09-01 11:00:12'),
  (2,1,1002,'2013-09-02 12:50:12'),
  (2,1,1002,'2013-09-05 13:50:12'),
  (1,1,1002,'2013-09-06 11:50:12'),
  (1,1,1002,'2013-09-07 12:50:12'),
  (2,1,1002,'2013-09-15 13:50:12');
4

3 回答 3

0

这很简单。您必须使用 CASE 语句分别计算主站点和非主站点访问。

查询 1

select userid
  from (
  select userid,
         count(case when visitsiteid = homesiteid then 1 end) homesitecount,   --count only those records where home site = visited site
         count(case when visitsiteid != homesiteid then 1 end) nonhomesitecount  --count only those records where home site != visited site
    from visit
   group by userid) as groupedvisit             --use this as inline view and filter out the users. 
 where nonhomesitecount >= homesitecount;

查询 2

select userid,
       count(case when visitsiteid != homesiteid then 1 end) nonhomesitecount
  from visit
 group by userid
having count(case when visitsiteid != homesiteid then 1 end) > 3; 

下一个查询类似于 Query1 和 2。您必须添加一个额外的 where 子句来限制日期。

where time >= lower_limit and time <= upper_limit

sqlfiddle演示

于 2013-10-01T04:26:04.193 回答
0

更新:如果正确理解你想要什么,你可以这样做

1. 返回所有非主页访问次数 >= 主页访问次数的用户;任何日期

SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 GROUP BY userid
HAVING visitsnonhome >= visitshome;

2. 返回所有非家庭站点访问计数 >= 输入的最小整数的用户

SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 GROUP BY userid
HAVING visitsnonhome >= 3;

3. 1 和 2 都有日期范围...

只需在WHERE上述查询中添加一个子句

SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 WHERE time >= '2013-09-01 11:50:12' 
   AND time <= '2013-09-13 15:48:12'
 GROUP BY userid
HAVING visitsnonhome >= visitshome;

SELECT userid, 
       SUM(visitsiteid <> homesiteid) visitsnonhome,
       SUM(visitsiteid = homesiteid) visitshome
  FROM visit
 WHERE time >= '2013-09-01 11:50:12' 
   AND time <= '2013-09-13 15:48:12'
 GROUP BY userid
HAVING visitsnonhome >= 3;

这是SQLFiddle演示

于 2013-10-01T04:26:17.783 回答
0

没有试过和测试..

SELECT Count(visitId) AS Visits, userId 
    FROM homeSiteId 
    GROUP BY visitId HAVING Count(visitId) > 2999
于 2013-10-01T03:26:28.200 回答