根据每组的记录过滤分组

时间:2021-06-07 04:50:13

标签: mysql sql

我正在使用 mysql Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

我创建了以下数据库:

CREATE TABLE `transaction` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `company` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `filling_date` timestamp NULL DEFAULT NULL,
  `trx_type` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `amount_range` int DEFAULT NULL,
  `insider_price` decimal(30,4) DEFAULT NULL,
  `qty` int DEFAULT NULL,
  `transaction_value` decimal(30,4) DEFAULT NULL,
  PRIMARY KEY (`id`)
); 

INSERT INTO transaction (id, company, filling_date, trx_type, amount_range, insider_price, qty, transaction_value) VALUES(6, 'Apple', '2021-06-06 16:39:54.000', 'P - Purchase', 1000, 10, 100, 8000);
INSERT INTO transaction (id, company, filling_date, trx_type, amount_range, insider_price, qty, transaction_value) VALUES(7, 'Apple', '2021-06-05 15:29:34.000', 'S - Sale', 2000, 11, 200, 9000);
INSERT INTO transaction (id, company, filling_date, trx_type, amount_range, insider_price, qty, transaction_value) VALUES(8, 'Microsoft', '2021-06-05 11:22:15.000', 'P - Purchase', 2000, 10, 500, 1000);
INSERT INTO transaction (id, company, filling_date, trx_type, amount_range, insider_price, qty, transaction_value) VALUES(9, 'Apple', '2021-05-16 11:29:44.000', 'P - Purchase', 1000, 11, 1000, 10000);
INSERT INTO transaction (id, company, filling_date, trx_type, amount_range, insider_price, qty, transaction_value) VALUES(10, 'Microsoft', '2021-01-10 11:22:15.000', 'P - Purchase', 100, 30, 700, 3000);
INSERT INTO transaction (id, company, filling_date, trx_type, amount_range, insider_price, qty, transaction_value) VALUES(11, 'Microsoft', '2021-06-01 22:22:15.000', 'S - Sale', 6000, 60, 600, 4000);

我想过滤我的 group-by 语句,以便按 > 1 (count(Company) > 1) 获取每组的所有记录。

我尝试了以下方法:

select 
  count(Company) AS RecordsPerGroup,
  Company, 
  max(filling_date) Last_filling_date, 
  trx_type,
  sum(amount_range) amount_range,
  sum(insider_price) insider_price,
  sum(qty) qty,
  sum(transaction_value) transaction_value
from transaction
where filling_date >= DATE(NOW()) - INTERVAL 31 DAY 
AND trx_type ='p - purchase'
AND count(Company) > 1
group by company

当我运行上述查询时,我得到:

Query Error: Error: ER_INVALID_GROUP_FUNC_USE: Invalid use of group function

错误源于以下语句 AND count(Company) > 1,这在我的查询中是不允许的。

在我的 db-fiddle 示例下面找到:

DB-Fiddle Example

我想得到以下输出:

| RecordsPerGroup | Last_filling_date   | amount_range | insider_price | qty  | transaction_value | Company   | trx_type     |
| --------------- | ------------------- | ------------ | ------------- | ---- | ----------------- | --------- | ------------ |
| 2               | 2021-06-06 16:39:54 | 2000         | 21.0000       | 1100 | 18000.0000        | Apple     | P - Purchase |

关于如何过滤我的 group-by 语句以仅显示具有 >2 个 group bys 的所有记录的任何建议?

感谢您的回复!

2 个答案:

答案 0 :(得分:1)

关于计数的断言属于HAVING子句,而不是WHERE子句,而且如果你打算选择trx_type,那么这个列也应该出现在{{ 1}} 子句:

GROUP BY

答案 1 :(得分:1)

当您使用 GROUP BY,并且希望将聚合视为整体“过滤器”条件的一部分时,请使用 HAVING。

例如

SELECT
  count(Company) AS RecordsPerGroup,
  Company, 
  max(filling_date) Last_filling_date, 
  trx_type,
  sum(amount_range) amount_range,
  sum(insider_price) insider_price,
  sum(qty) qty,
  sum(transaction_value) transaction_value
from transaction
where filling_date >= DATE(NOW()) - INTERVAL 31 DAY 
AND trx_type ='p - purchase'
group by company
HAVING COUNT(Company) > 1

查看 MySql 文档以获取更多参考https://www.mysqltutorial.org/mysql-having.aspx

<块引用>

HAVING 子句与 WHERE 子句一样,指定选择 状况。 WHERE 子句指定列的条件 选择列表,但不能引用聚合函数。拥有 子句指定组的条件,通常由 GROUP 形成 BY 子句。查询结果只包含满足 HAVING 的组 状况。 (如果不存在 GROUP BY,则所有行都隐式地形成一个 单个聚合组。)

HAVING 子句几乎最后应用,就在项目发送之前 给客户端,没有优化。 (LIMIT 在 HAVING 之后应用。)

SQL 标准要求 HAVING 必须仅引用 GROUP BY 子句或聚合函数中使用的列。然而, MySQL 支持此行为的扩展,并允许 HAVING 将 SELECT 列表中的列和外部子查询中的列称为 嗯。

https://dev.mysql.com/doc/refman/8.0/en/select.html