I have below table with Boolean column has_object which indicate each row has associated digital object or not.
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| pid | varchar(255) | NO | PRI | | |
| title | text | YES | | NULL | |
| owner_uid | int(11) | YES | | NULL | |
| has_object | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
I have tried this query to obtain statistical information about each owner_uid. but in my table it returns wrong result:
SELECT
a.owner_uid,
count(b.pid) as count1,
count(c.pid) as count2
FROM
islandora_report a
JOIN islandora_report b ON b.owner_uid = a.owner_uid AND b.has_object = 0
JOIN islandora_report c ON c.owner_uid = a.owner_uid AND c.has_object = 1
GROUP BY a.owner_uid;
The result:

Since the
BOOLEANis merely a0or1, you can actually do one pair ofSUM()without any joins to add up the column.There are other ways to invert the boolean than the method above. It’s just the first that came to mind. You could also subtract the true sum from the total count, for example: