Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Query matching same values in same column based on criteria

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading