【问题标题】:How to count total number of records after join the three tables in postgresql?在postgresql中加入三个表后如何计算总记录数?
【发布时间】:2018-12-28 06:54:45
【问题描述】:

我有一个查询,它在执行后给我总共 12408 条记录,但我希望这给我作为计数列的总记录

选择 c.complaint_id,c.server_time,c.completion_date,c.road_id,c.photo,c.dept_code,c.dist_code,c.eng_userid,c.feedback_type,c.status,p.dist_name,p.road_name,p. road_dept,e.display_name,e.mobile 从投诉为 c INNER JOIN pwd_roads as p ON p.road_id=c.road_id INNER JOIN enc_details as e ON CAST(e.enc_code as INTEGER) = p.enccode 其中 c.complaint_id=c.parent_complaint_id 和 c.dept_code='PWDBnR' 和 '2018-09-03' 和 '2018-12-19' 之间的 c.server_time

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    您可以使用窗口函数解决此问题。例如,如果您希望您的第一列是 SELECT 语句完成的总行数:

    select count(1) over(range between unbounded preceding and unbounded following) as total_row_count
     , c.complaint_id,c.server_time,c.completion_date,c.road_id,c.photo,c.dept_code,c.dist_code,c.eng_userid,c.feedback_type,c.status,p.dist_name,p.road_name,p.road_dept,e.display_name,e.mobile from complaints as c INNER JOIN pwd_roads as p ON p.road_id=c.road_id INNER JOIN enc_details as e ON CAST(e.enc_code as INTEGER) = p.enccode where c.complaint_id=c.parent_complaint_id and c.dept_code='PWDBnR' and c.server_time between '2018-09-03' and '2018-12-19'
    

    请注意,如果使用窗口函数,则在 LIMIT 子句之前评估窗口函数,因此如果您要将 LIMIT 100 添加到查询中,它可能会给出大于 100 的行数,即使最多 100 行也会被退回。

    【讨论】:

      【解决方案2】:

      最简单但不是很优雅的方法是:

      select count(*)
      from
      ( 
      select c.complaint_id,c.server_time,c.completion_date,c.road_id,c.photo,c.dept_code,c.dist_code,c.eng_userid,c.feedback_type,c.status,p.dist_name,p.road_name,p.road_dept,e.display_name,e.mobile from complaints as c INNER JOIN pwd_roads as p ON p.road_id=c.road_id INNER JOIN enc_details as e ON CAST(e.enc_code as INTEGER) = p.enccode where c.complaint_id=c.parent_complaint_id and c.dept_code='PWDBnR' and c.server_time between '2018-09-03' and '2018-12-19'
      )
      

      【讨论】:

        猜你喜欢
        • 2015-11-22
        • 1970-01-01
        • 1970-01-01
        • 2011-02-11
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-07
        相关资源
        最近更新 更多