I have a CCK field that references users (using CCK), as a method of keeping attendance at meetings. I know this is not the most effective way of doing it - but I set it up a while ago when I knew a lot less about Drupal. I would like to create a view that returns the number referenced users for attendance purposes. In order to do so, I did the following:
Added a Filter: "Node: Type = Event"
Added fields:
"Node: Title"
"Content: Attendance - delta" (This field does not group multiple values, so there is a row for each referenced user)
"SQL Aggregation: Group by Fields"
and set it up to group by Node: Title, and aggregate Count on Content: Attendance - delta. (I chose delta because if I just chose the user, it was replacing the Count function with the user that had the corresponding ID).
When I ran the query, I received just a table, with all the rows still included, and a 1 replacing the delta. After looking at the generated SQL command, and messing around in phpMyAdmin for a while, I figured out where the problem was.
Here is what was being generated
SELECT
node.nid AS nid,
node.title AS node_title,
COUNT(node_data_field_attendance.delta) AS node_data_field_attendance_delta,
node_data_field_attendance.field_attendance_uid AS node_data_field_attendance_field_attendance_uid
FROM node node
LEFT JOIN content_field_attendance node_data_field_attendance ON node.vid = node_data_field_attendance.vid
WHERE node.type in ('event')
GROUP BY node_title, nid, node_data_field_attendance_field_attendance_uid
ORDER BY node_title DESC
Notice in the GROUP BY section, the following field is added: node_data_field_attendance_field_attendance_uid. It appears that when the delta field is selected, or vice versa, the other field is also included in the query. Therefore, the group by is not going to be effective, as the aggregate function is effective working on a field that is also in the group by. To test this theory, I tried the code:
SELECT
node.nid AS nid,
node.title AS node_title,
COUNT(node_data_field_attendance.delta) AS node_data_field_attendance_delta,
node_data_field_attendance.field_attendance_uid AS node_data_field_attendance_field_attendance_uid
FROM node node
LEFT JOIN content_field_attendance node_data_field_attendance ON node.vid = node_data_field_attendance.vid
WHERE node.type in ('event')
GROUP BY node_title, nid
ORDER BY node_title DESC
(simply removing the nid, node_data_field_attendance_field_attendance_uid from the group by, and it was successful. Any suggestions on how to proceed?
Comments
Comment #1
nate.klingerman commentedFunny, I have been trying to fix this for hours, but its not until 10 mins after I finally report the issue that I find a possible solution.... I hate how it always works that way.
Anyway, I decided to select both the delta and non-delta fields, and then grouped all the other fields, and counted both the user reference fields. I do not know if this should just be a documented solution, or if a fix should be made, so I am going to leave the status active.
My guess is, however, that this will work for node references as well.