[英]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.