MySQL查询以获得用户和组的总和

时间:2017-10-30 11:00:56

标签: mysql

我有两个表user_profile和tracked_search。 user_profile表具有用户详细信息和tracked_search跟踪用户进行的搜索。

每当用户进行搜索时,此搜索条目都会进入tracked_search表。如果没有搜索特定日期的内容,则tracked_search中不会添加任何内容。

我需要制作一份报告,其中我需要显示每周过去6个月的数据 例如

Date       week_count   user_count
2017-05-01    18           10
2017-05-08    19           50
    .
    .
2017-07-03   27            80
    .
2017-10-2    40           20
   .
   .
2017-10-23   43           40

数据应按周分组,user_count是搜索该周的不同用户的数量。 即使某一周没有数据,也应将周日,周数和用户数打印为0

我的表格如下

User_profile
user_id  user_name user_emailId        user_passsword   user_role creation_date 
  1        Mac       mac@yahoo.com      password123       USER     23/10/2017
  2        Shane     Shane@yahoo.com    password123       USER     23/10/2017

  Tracked_search
  id      created             content  search_term,  user_id
014af54e 2017-10-15 18:36:49  ARTICLE     latest       1
08f55f2d 2017-10-18 18:34:04  EVENT       upcoming     1
1e74f026 2017-10-25 18:37:11  DISCUSSION  newest       2
20075e4a 2017-10-22 18:35:41  ARTICLE    latest        1
22cde973 2017-10-17 18:36:49  ARTICLE     latest       2
2d1d3314 2017-10-16 18:36:49  ARTICLE     latest       2

2 个答案:

答案 0 :(得分:1)

这是基于您对其他问题的my answer

假设周模式3,因为基于你的问题,你似乎想要在星期一开始几周。但也许您想要模式1,5或7(参见WEEK()):

SELECT weeks.day, weeks.yearweek % 100 AS week, COUNT(DISTINCT ts.user_id) AS user_count
FROM (
    SELECT MIN(ADDDATE('1970-01-01', t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i)) AS day,
           YEARWEEK(ADDDATE('1970-01-01', t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i), 3) AS yearweek
    FROM (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS t0,
         (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS t1,
         (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS t2,
         (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS t3,
         (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS t4
    GROUP BY YEARWEEK(ADDDATE('1970-01-01', t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i), 3)
) AS weeks
LEFT OUTER JOIN tracked_search AS ts
    ON weeks.yearweek = YEARWEEK(ts.created, 3)
WHERE weeks.day >= '2017-05-01'
AND weeks.day < '2017-10-30'
GROUP BY weeks.yearweek;

使用问题中的示例数据,它会返回以下内容:

+------------+------+------------+
| day        | week | user_count |
+------------+------+------------+
| 2017-05-01 |   18 |          0 |
| 2017-05-08 |   19 |          0 |
| 2017-05-15 |   20 |          0 |
| 2017-05-22 |   21 |          0 |
| 2017-05-29 |   22 |          0 |
| 2017-06-05 |   23 |          0 |
| 2017-06-12 |   24 |          0 |
| 2017-06-19 |   25 |          0 |
| 2017-06-26 |   26 |          0 |
| 2017-07-03 |   27 |          0 |
| 2017-07-10 |   28 |          0 |
| 2017-07-17 |   29 |          0 |
| 2017-07-24 |   30 |          0 |
| 2017-07-31 |   31 |          0 |
| 2017-08-07 |   32 |          0 |
| 2017-08-14 |   33 |          0 |
| 2017-08-21 |   34 |          0 |
| 2017-08-28 |   35 |          0 |
| 2017-09-04 |   36 |          0 |
| 2017-09-11 |   37 |          0 |
| 2017-09-18 |   38 |          0 |
| 2017-09-25 |   39 |          0 |
| 2017-10-02 |   40 |          0 |
| 2017-10-09 |   41 |          1 |
| 2017-10-16 |   42 |          2 |
| 2017-10-23 |   43 |          1 |
+------------+------+------------+

答案 1 :(得分:0)

我能够使用以下查询解决此问题。希望它会帮助某人

select 
t1.attempt_date,
coalesce(SUM(t1.attempt_count+t2.attempt_count), 0) AS attempt_count
from
(
  select DATE_FORMAT(a.Date,'%Y/%m/%d') as attempt_date,
  '0' as  attempt_count
  from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
  ) a
  where a.Date BETWEEN NOW() - INTERVAL 6 MONTH AND NOW()
  group by SUBDATE(a.Date, WEEKDAY(a.Date - INTERVAL 1 day)) 
)t1
left join
(
  select count(t1.user_count) as attempt_count, t1.created_date FROM (select distinct ts.user_id as user_count, date(ts.created) as created_date from tracked_search ts
inner join user_profile up ON up.id = ts.user_id
group by ts.created
order by ts.created) t1
Group by SUBDATE(date(t1.created_date), WEEKDAY(date(t1.created_date)))
)t2
on t2.created_date = t1.attempt_date
group by DAY(t1.attempt_date)
order by t1.attempt_date desc;