Three tables
cats
=============
id
cat_herder_id
size
color
birthday
collars
=============
id
cat_id
diameter
color
material
cat_herders
=============
id
name
age
height
I want to get a row for each cat herder that is over a meter tall with a count of the number of cats that were born in February that are orange with black collars that belong to that herder and a count of all the cats that are orange with blue collars the belong to each herder, how would I go about doing a query of that sort.
I don’t think I can simply specify in the where statement because my count appears to be off, I am grouping by cat_herders.id
EDIT: Less sanitized, less abstracted version of what I have so far:
SELECT company.tblusers.first_name, company.tblusers.last_name, company.tblusers.userid, SUM(db.tasks.estimated_nonrecurring+db.tasks.estimated_recurring), COUNT(sugarcrm2.ncr_ncr.id),
SUM(db.batch_log.time_elapsed) FROM company.tblusers
INNER JOIN db.batch_log ON company.tblusers.userid = db.batch_log.userid
INNER JOIN db.tasks ON db.batch_log.batch_id = db.tasks.batch_id
INNER JOIN sugarcrm2.ncr_ncr ON company.tblusers.first_name + " " + company.tblusers.first_name = sugarcrm2.ncr_ncr.employee
WHERE departmentid = 8 AND DATE(db.batch_log.start_time) = DATE(NOW()) GROUP BY userid
Note: Depending on your RDBMS and the datatype of the
cats.birthdaycolumn, you’ll probably need to adjust this query to extract the birth month, but this gives you the general idea.