I'm a little stuck with a MySQL query and need some help.
Let's assume we have a table "animals" with these columns:
animal_id | animal_species
1 | dog
2 | cat
3 | dog
4 | pig
Furthermore we have a table "examinations" that includes various examinations on these animals:
examination_id | examination_animal_id
1 | 1
2 | 1
3 | 3
4 | 2
5 | 4
Finally we have another table "diagnoses_statistics" that contains one or multiple diagnoses for some examinations:
diagnoses_statistics_id | diagnoses_statistics_examination_id | diagnoses_statistics_diagnosis
1 | 1 | luxation
2 | 3 | thrombopathy
3 | 3 | trauma
4 | 3 | luxation
5 | 5 | trauma
All tables contain many other columns, I just shortened them as above examples should be enough to illustrate my question.
We want to know how many identical diagnoses there are per species. To do this, we could run this query:
SELECT animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis,
COUNT(diagnoses_statistics.diagnoses_statistics_diagnosis) AS diagnoses_count
FROM examinations
JOIN animals ON animals.animal_id = examinations.examination_animal_id
JOIN diagnoses_statistics ON diagnoses_statistics.diagnoses_statistics_examination_id = examinations.examination_id
GROUP BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
ORDER BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
This gives us something like this:
animal_species | diagnoses_statistics_diagnosis | diagnoses_count
dog | luxation | 2
dog | thrombopathy | 1
dog | trauma | 1
pig | trauma | 1
So far, so good. Now we want to add a column with a sum of every species that contains any diagnosis:
animal_species | diagnoses_statistics_diagnosis | diagnoses_count | species_count
dog | luxation | 2 | 2
dog | thrombopathy | 1 | 2
dog | trauma | 1 | 2
pig | trauma | 1 | 1
To achieve this, I tried this code which seems to produce correct values in "species_count", but it breaks the column "diagnoses_count":
SELECT animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis,
COUNT(diagnoses_statistics.diagnoses_statistics_diagnosis) AS diagnoses_count,
species_count.species_count
FROM examinations
JOIN animals ON animals.animal_id = examinations.examination_animal_id
JOIN diagnoses_statistics ON diagnoses_statistics.diagnoses_statistics_examination_id = examinations.examination_id
JOIN
(SELECT animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis,
COUNT(animals.animal_id) AS species_count
FROM examinations
JOIN animals ON animals.animal_id = examinations.examination_animal_id
JOIN diagnoses_statistics ON diagnoses_statistics.diagnoses_statistics_examination_id = examinations.examination_id
GROUP BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
) AS species_count ON species_count.animal_species = animals.animal_species
GROUP BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
ORDER BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
Maybe I am doing this completely wrong.
Can you please help me how to solve this?
Thank you very much for your help in advance.
count(distinct x)helpful. You can do things likecount(distinct aminal_id)orcount(distinct diagnosis_statistics_id).