[英]How to use count with Distinct ON
我有这个查询已经有效,我需要为此写计数查询(需要分页),并且我在使用DISTINCT ON时使用count有问题,任何人都知道正确的语法和使用方法,我已经用谷歌搜索了,但是找不到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
,则返回相同的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.