我有这个查询已经有效,我需要为此编写计数查询(需要分页),并且我在使用带有 DISTINCT ON 的计数时遇到问题,任何人都知道正确的语法和使用它的方法,我已经用谷歌搜索但没有能够找到任何带有 DISTINCT ON 的计数查询
结果我期望这个查询的总行数已经有效
select DISTINCT on (csd.team_contest_id)
c.id as contestId
from contest as c
left join company_sponsor_team as csd on csd.contest_id = c.id
left join sponsor as s on s.id = c.sponsor_id
left join team as d on d.id = c.team_id
left join player as m on c.creator_id = m.id
WHERE c.id = c.id
AND ((c.team_id is not null and c.sponsor_id is null and lower(d.name) LIKE LOWER(CONCAT('%TAN%')))
OR (c.team_id is not null and c.sponsor_id is not null and lower(s.name) LIKE LOWER(CONCAT('%TAN%')))
OR (c.team_id is null and lower(s.name) LIKE LOWER(CONCAT('%TAN%')))) AND (CURRENT_DATE < c.archive_date)
最佳答案
如果要统计行数,使用子查询:
select count(*)
from ( <your query here> ) x;
如果你厌恶子查询,你总是可以这样做:
select count(distinct csd.team_contest_id)
from . . . <the rest of your query here>;
假设 csd.team_contest_id
不是 NULL
,这将返回相同的值。
关于sql - 如何使用 Distinct ON 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48141352/