使用静态最大日期和最小日期查找每日会话的平均值

Finding Average of Daily Sessions with a Static Max Date and Min Date

PHP 5.X, MySQL 5.7.15

获取按星期几分组的数据,以使用最大日期和最小日期提供每日使用的平均会话数。在这种情况下,最大和最小日期应用于星期几,这导致每个案例的周数不同。

构建这些结果的查询定义为下面的查询 1。本质上,最后 4 列中的信息应该是静态的,我使用 SELF JOIN 完成了此操作,详情如下。在那个例子中,session_count 爆炸了,因为我只在开发环境中工作。

那么哪里出了问题,为什么?

  session_count | weekday | maxDate             |minDate              | numOfDayDiff| numOfWeeks
'29'            | 'Friday'|'2017-03-10 12:16:47'|'2016-08-12 12:31:28'| '210'       | '30'
'26'            |'Monday' |'2017-03-06 17:10:59'|'2016-08-08 14:31:16'| '210'       | '30'
'6'            |'Saturday'|'2017-03-04 23:26:12'|'2016-08-20 23:10:47'| '196'       | '28'
'10'            |'Sunday' |'2017-03-12 18:28:51'|'2016-08-14 16:26:30'| '210'       | '30'
'22'           |'Thursday'|'2017-03-09 15:45:27'|'2016-06-30 12:42:19'| '252'       | '36'
'22'            |'Tuesday'|'2017-03-21 15:00:08'|'2016-06-28 19:51:21'| '266'       | '38'
'22'          |'Wednesday'|'2017-03-01 23:57:34'|'2016-07-06 16:17:39'| '238'       | '34'

我希望最大日期和最小日期是静态的。我尝试使用 w3schools 对 Self JOIN Syntax

的解释
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

我尝试应用此查询,maxDate、minDate、numDaysDiff 和 numOfWeeks 与上述最高周数的值一致,但 session_count[=43 的统计数据=] 爆炸了。

这里是查询1得到上面的table:

SELECT
CEIL(COUNT(ss.session_id) / (FLOOR (DATEDIFF(MAX(ss.date),MIN(ss.date))/7))) as session_count,
DAYNAME((ss.date)) as weekday,
MAX(ss.date) as maxDate,
MIN(ss.date) as minDate,
DATEDIFF(MAX(ss.date),MIN(ss.date)) as numOfDayDiff,
FLOOR(DATEDIFF(MAX(ss.date),MIN(ss.date))/7) as numOfWeeks
FROM  session ss
JOIN user u ON ss.user = u.id
JOIN account a on u.account = a.id
WHERE a.isTest=false
GROUP BY weekday
ORDER BY weekday;

这里是 Q-2 使用 SELF JOIN 语法修改后的输出 table:

SELECT
CEIL(COUNT(ss.session_id) / (FLOOR (DATEDIFF(MAX(xx.date),MIN(xx.date))/7))) as session_count,
DAYNAME((ss.date)) as weekday,
MAX(xx.date) as maxDate,
MIN(xx.date) as minDate,
DATEDIFF(MAX(xx.date),MIN(xx.date)) as numOfDayDiff,
FLOOR(DATEDIFF(MAX(xx.date),MIN(xx.date))/7) as numOfWeeks
FROM  session xx, session ss
JOIN user u ON ss.user = u.id
JOIN account a on u.account = a.id
WHERE a.isTest=false
GROUP BY weekday
ORDER BY weekday;

结果是:

session_count | weekday | maxDate             |minDate              | numOfDayDiff| numOfWeeks
99357         |Friday   |2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38
88062         |Monday   |2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38
16829         |Saturday |2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38
32505         |Sunday   |2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38
90136         |Thursday |2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38
94516         |Tuesday  |2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38
83451         |Wednesday|2017-03-21 15:00:08  |2016-06-28 19:51:21  |266          |38

任何人都可以告诉我我是如何犯错的吗?我将看看是否可以创建一个 mysql fiddle 作为示例附在此处。

为了减轻数据库的压力,我决定使用 查询 1 的修改版本,并使用结果来确定哪个周数最多并划分计数按 php.

中的周数

这里是修改后的查询1

SELECT
COUNT(ss.session_id) as session_count,
DAYNAME((ss.date)) as weekday,
MAX(ss.date) as maxDate,
MIN(ss.date) as minDate,
CEIL(DATEDIFF(MAX(ss.date),MIN(ss.date))/7) as numOfWeeks
FROM  session ss
JOIN user u ON ss.user = u.id
JOIN account a on u.account = a.id
WHERE a.isTest=false
GROUP BY weekday
ORDER BY weekday;

session_count | weekday | maxDate            | minDate           | numOfWeeks
2017          | Friday  |2017-03-17 23:39:56 |2016-09-09 01:06:36|  27
1908          | Monday  |2017-03-20 23:59:53 |2016-09-12 14:52:29|  27
943           | Saturday|2017-03-18 23:39:14 |2016-09-17 14:49:56|  26
825           | Sunday  |2017-03-19 23:56:37 |2016-09-18 22:28:45|  26
2429          | Thursday|2017-03-16 23:51:23 |2016-09-08 12:02:54|  27
2341          | Tuesday |2017-03-21 23:52:20 |2016-09-06 12:06:39|  28
2569          |Wednesday|2017-03-22 17:03:16 |2016-09-07 02:43:10|  28

注意数据库不同数据不同