(Postgres)
I'm not clear on the following: I'm selecting various fields from STUDY_T which may be sub-selects with aggregate functions. One independent field I'm retrieving is a LOOKUP_T join, lookupStudyType.description, which is not related to any aggregate functions. But I'm getting the error
ERROR: column "lookupstudytype.description" must appear in the GROUP BY clause or be used in an aggregate function
LINE 3: lookupStudyType.description AS studyTypeDescription,...
Why do I need to also group not just by s.id but also lookupStudyType.description (while the other s. groupings are not required)?
SELECT
s.id AS id,
lookupStudyType.description AS studyTypeDescription,
s.name AS name,
s.abbreviation AS abbreviation,
s.start_date AS startDate,
s.end_date AS endDate,
(SELECT COUNT(r.id)
FILTER
(WHERE r.status_id IN (76, 77) )) AS recallCount,
(SELECT COUNT(DISTINCT sp.id)) AS participantCount,
(SELECT MAX(r.created_date)
FILTER
(WHERE r.status_id IN (76,77) )) AS lastRecall,
s.login_access_required AS loginAccessRequired,
s.description AS description,
s.custom_participant_exit_message AS customParticipantExitMessage
FROM study_t s
INNER JOIN lookup_t lookupStudyType
ON s.study_type_id = lookupStudyType.id
INNER JOIN study_staff_t ss
ON s.id = ss.study_id
INNER JOIN users_t u
ON ss.researcher_id = u.id
LEFT JOIN study_participants_t sp
ON s.id = sp.study_id
LEFT JOIN recalls_t r
ON r.user_id = sp.user_id
WHERE u.user_name = 'test@test.com'
GROUP BY
s.id
ORDER BY s.abbreviation ASC