I have a table in MySQL as follows.
| donorId | amount | year |
|---|---|---|
| 787 | 9.5 | 2022 |
| 567 | 7.9 | 2021 |
| 787 | 30 | 2022 |
| 456 | 2.5 | 2022 |
| 567 | 26 | 2022 |
| 456 | 26 | 2022 |
I need to find all donors who made at least two constructive in 2022(787, 456). there is also an issue: I can’t use HAVING in the query. How to do this?
>Solution :
There is example how to do this without having in your query, using subqueries
declare
@year int = '2022'
select
x.donorId
from (
select
count(1) c,
t.donorId donorId
from yourtable t
where t.year = @year
group by
t.donorId
) x
where x.c > 1