必须从我的postgresql查询中查找行数

时间:2017-07-04 07:32:34

标签: postgresql

您好我尝试从以下查询中获取行数:

        select count(substring(wsresult_question FROM '[0-9]+') as pumporder) AS totals,
            job_id,
            job_siteid,
            job_completed
            from webserviceresults w, jobs s 
            where job_siteid = '1401' 
            and job_id = wsresult_jobid
            and job_completed is not null
            and wsresult_question LIKE 'job.job_site_data.site_meters.pump.%' 
            and wsresult_category = 'Job' 
            group by pumporder,job_id,job_siteid,job_completed order by job_completed desc

我试过这个,我得到了像

这样的错误
There was an SQL error: 
ERROR: syntax error at or near "as" LINE 1: ... count(substring(wsresult_question FROM '[0-9]+') as pumpord... ^

在这一行substring(wsresult_question FROM '[0-9]+') as pumporder中,我只是厌倦了从一些连接字符串中获取一个数字。连接字符串就像

  

1.job.job_site_data.site_meters.pump.0.meter_calibration_record.meter_adjustedtofast

     

2.job.job_site_data.site_meters.pump.0.meter_calibration_record.meter_adjustedtoslow

     

3.job.job_site_data.site_meters.pump.1.meter_calibration_record.meter_adjustedtofast

所以substring(wsresult_question FROM '[0-9]+') as pumporder返回数组中的数字,如0,1。我现在需要总计行数。所以请帮助我。

如果您有任何疑问,请与我们联系。

提前致谢!

1 个答案:

答案 0 :(得分:1)

您的错误意味着您不应为该功能创建别名 - 仅适用于该列,因此如果您从as pumporder移除count(substring(wsresult_question FROM '[0-9]+') as pumporder),则错误将消失

你的方法非常值得怀疑。如果您想用substring(wsresult_question FROM '[0-9]+')计算行数,最好改为:

    select count(1) AS totals,
        job_id,
        job_siteid,
        job_completed
        from webserviceresults w, jobs s 
        where job_siteid = '1401' 
        and job_id = wsresult_jobid
        and job_completed is not null
        and wsresult_question  ~ '^(job.job_site_data.site_meters.pump.)[0-9]' 
        and wsresult_category = 'Job' 
        group by pumporder,job_id,job_siteid,job_completed order by job_completed desc

最后字符串job.job_site_data.site_meters.pump.0看起来像json路径,所以使用json数组长度函数更合适,不计算行