title: Filter group-by based on records per group
body: <p>I am using <code>mysql  Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))</code>.</p>
  <p>I have created the following database:</p>
  <pre><code>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);
  
  </code></pre>
  <p>I would like to filter my group-by statement so that I get all records per group by &gt; 1 (<code>count(Company) &gt; 1</code>).</p>
  <p>I tried the following:</p>
  <pre><code>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 &gt;= DATE(NOW()) - INTERVAL 31 DAY 
  AND trx_type ='p - purchase'
  AND count(Company) &gt; 1
  group by company
  </code></pre>
  <p>When I run the above query I get:</p>
  <pre><code>Query Error: Error: ER_INVALID_GROUP_FUNC_USE: Invalid use of group function
  
  </code></pre>
  <p>The error stems from the following statement <code>AND count(Company) &gt; 1</code>, which is not allowed in my query.</p>
  <p>Find below my db-fiddle example:</p>
  <p><a href="https://www.db-fiddle.com/f/h6oqWuNiaRHP44ebNfjMC7/5" rel="nofollow noreferrer">DB-Fiddle Example</a></p>
  <p>I would like to get the following output:</p>
  <pre><code>| 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 |
  </code></pre>
  <p>Any suggestions how to filter my group-by statements to display only all records that have &gt;2 group bys?</p>
  <p>I appreciate your replies!</p>