(Postgres)
我不太清楚以下内容:我从STUDY_T
选择各种字段,其中可能有使用聚合函数的子查询。我正在检索一个独立的字段,这是一个LOOKUP_T连接:lookupStudyType.description
,它与任何聚合函数都没有关联。但我收到了错误提示:
ERROR: column "lookupstudytype.description" must appear in the GROUP BY clause or be used in an aggregate function
LINE 3: lookupStudyType.description AS studyTypeDescription,...
为什么我需要按照
s.id
和lookupStudyType.description
进行分组(而其他s.
分组不是必需的)? 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
STUDY_T
列不需要分组呢?没有其他s.
字段是必需的。 - gene b.