unstat()
returns setof "user"
as $$
begin
SELECT
"university".id,
"university".name,
"university".students_number,
COUNT(DISTINCT "user".chat_id) as "students_total",
(
SELECT COUNT(DISTINCT "user".chat_id) FROM "user"
WHERE
"user".time_registered > (CURRENT_DATE - interval '24 hour')
AND
"user".university_id = "university".id
) as "students_new",
(
SELECT COUNT(DISTINCT "user_action".chat_id) FROM "user_action"
JOIN "user" ON "user".chat_id = "user_action".chat_id
WHERE
"user_action".time_clicked > (CURRENT_DATE - interval '24 hour')
AND
"user".university_id = "university".id
) as "students_active"
FROM "user"
JOIN "university" ON "user".university_id = "university".id
GROUP BY "university".id;
end;
$$
language plpgsql;
Убрать begin/end и заменить language на sql
Обсуждают сегодня