计数,但函数mysql的使用组无效
我有一张这样的桌子计数,但函数mysql的使用组无效,mysql,mysql-workbench,mysql-select-db,Mysql,Mysql Workbench,Mysql Select Db,我有一张这样的桌子 CREATE TABLE order_match (`order_buyer_id` int, `createdby` int, `createdAt` datetime, `quantity` decimal(10,2)) ; INSERT INTO order_match (`order_buyer_id`, `createdby`, `createdAt`, `quantity`) VALUES (19123, 19, '2017-02-02'
CREATE TABLE order_match
(`order_buyer_id` int, `createdby` int, `createdAt` datetime, `quantity` decimal(10,2))
;
INSERT INTO order_match
(`order_buyer_id`, `createdby`, `createdAt`, `quantity`)
VALUES
(19123, 19, '2017-02-02', 5),
(193241, 19, '2017-02-03', 5),
(123123, 20, '2017-02-03', 1),
(32242, 20, '2017-02-04', 4),
(32434, 20, '2017-02-04', 5),
(2132131, 12, '2017-02-02', 6)
;
在此表中,order_buyer_id是交易的id,createdby是买家,createdAt是每笔交易的时间,quantity是交易的数量
我想找出每个重复订单的最大值、最小值、中值和平均值(交易>1的买家)
所以在这张表上,预期结果如下
+-----+-----+---------+--------+
| MAX | MIN | Average | Median |
+-----+-----+---------+--------+
| 3 | 2 | 2.5 | 3 |
+-----+-----+---------+--------+
注意:我使用的是mysql 5.7
我正在使用这种语法
select -- om.createdby, om.quantity, x1.count_
MAX(count(om.createdby)) AS max,
MIN(count(om.createdby)) AS min,
AVG(count(om.createdby)) AS average
from (select count(xx.count_) as count_
from (select count(createdby) as count_ from order_match
group by createdby
having count(createdby) > 1) xx
) x1,
(select createdby
from order_match
group by createdby
having count(createdby) > 1) yy,
order_match om
where yy.createdby = om.createdby
and om.createdAt <= '2017-02-04'
and EXISTS (select 1 from order_match om2
where om.createdby = om2.createdby
and om2.createdAt >= '2017-02-02'
and om2.createdAt <= '2017-02-04')
我们可以尝试通过
createdby
进行聚合,然后获取所需的聚合:
SELECT
MAX(cnt) AS MAX,
MIN(cnt) AS MIN,
AVG(cnt) AS Average
FROM
(
SELECT createdby, COUNT(*) AS cnt
FROM order_match
GROUP BY createdby
HAVING COUNT(*) > 0
) t
在MySQL 5.7中模拟中间层是一项非常艰巨的工作。如果你对中值有长期的需求,考虑升级到MySQL 8 + ./p>由19创建的两个数量级5。那么为什么你的期望最大值是3?不,先生,我不想看订单的数量,但看订单的频率,在这种情况下,由19个订单创建2次,你在用什么版本的MySQL?我用的是5.7。使用5.7计算中位数真的很痛苦。我应该添加哪一部分来插入这个语法:其中yy.createdby=om.createdby和om.createdAt='2017-02-02'和om2.createdAt我认为你应该编辑你的问题,并明确定义重复顺序基本上,数据虚拟是2017-02-02到2017-02-04之间的数据,在实际情况下,它是2017到现在之间的数据,因此我需要语法来定义不同的时间范围(不仅仅是2017-02-02到2017-02-04)您的语法几乎正确,但我需要显示时间范围的语法,以便我可以根据需要进行更改谢谢,先生,我只是用您的语法解决它,祝您愉快
SELECT
MAX(cnt) AS MAX,
MIN(cnt) AS MIN,
AVG(cnt) AS Average
FROM
(
SELECT createdby, COUNT(*) AS cnt
FROM order_match
GROUP BY createdby
HAVING COUNT(*) > 0
) t