I am trying to recreate the functionality of a query found inside of a mapfile on our mapserver into a plpgsql stored procedure.
Here is the query:
geom from (select g.gid, g.geom, g.basin, a.\"DATE\", a.\"VALUE\" from sarffg_basins_00_regional_3sec as g join \"%prod_table%\" as a on g.basin = a.\"BASIN\" where a.\"DATE\" = '%prod_date%') as subquery using unique gid using srid=4326
Within my stored procedure, I have:
RETURN QUERY
EXECUTE 'SELECT geom FROM (
SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN '||tablename_ts||' AS a
ON g.basin = a.basin
WHERE a.date = '''||adj_timestamp||''')
AS subquery USING UNIQUE gid USING srid=4326';
The above query found within my mapfile works fine. When I try calling my stored procedure inside of psql, I get:
ERROR: syntax error at or near "USING"
LINE 11: AS subquery USING UNIQUE gid USING srid=4326
^
QUERY: SELECT geom FROM (
SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN temp_table_ts AS a
ON g.basin = a.basin
WHERE a.date = '2017-01-15 00:00:00+00')
AS subquery USING UNIQUE gid USING srid=4326
CONTEXT: PL/pgSQL function ingest_ffgs_prod_composite_csv(text,bigint,boolean,boolean) line 239 at RETURN QUERY
I have also tried omitting the "using" clause within my function and instead leaving that part within the mapfile after my stored procedure is called, i.e.:
DATA "select * from ingest_ffgs_prod_composite_csv('%prod_table%', 1484438400) as subquery using unique gid using srid=4326"
With the stored procedure containing:
RETURN QUERY
EXECUTE 'SELECT geom FROM (
SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN '||tablename_ts||' AS a
ON g.basin = a.basin
WHERE a.date = '''||adj_timestamp||''');
But this leaves me with the error in my mapserver error log:
[Wed Jan 25 02:28:17 2017].593733 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_values'.
[Wed Jan 25 02:28:17 2017].659656 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: syntax error at or near "select"
LINE 1: ..._BASIN_TIMESERIES', 1484438400) as subquery where select * &...
^
[Wed Jan 25 02:28:17 2017].659862 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_product'.
[Wed Jan 25 02:28:22 2017].836950 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: syntax error at or near "select"
LINE 1: ..._BASIN_TIMESERIES', 1484438400) as subquery where select * &...
Finally, I tried leaving the front part of the query within the mapfile and only turning the subquery into the stored procedure:
mapfile:
DATA "geom from (select * from ingest_ffgs_prod_composite_csv('%prod_table%', 1484438400)) as subquery using unique gid using srid=4326"
stored procedure:
RETURN QUERY
EXECUTE 'SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN '||tablename_ts||' AS a
ON g.basin = a.basin
WHERE a.date = '''||adj_timestamp||''');
And this leaves me with:
[Wed Jan 25 02:35:36 2017].527302 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_values'.
[Wed Jan 25 02:35:36 2017].617289 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: column "VALUE" does not exist
LINE 1: select "VALUE",encode(ST_AsBinary(ST_Force2D("geom"),'NDR'),...
^
[Wed Jan 25 02:35:36 2017].617511 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_product'.
[Wed Jan 25 02:35:42 2017].103566 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: column "VALUE" does not exist
LINE 1: select "VALUE",encode(ST_AsBinary(ST_Force2D("geom"),'NDR'),...
The return statement being executed here is:
RETURN QUERY
EXECUTE 'SELECT g.'||quote_ident('gid')||',
g.'||quote_ident('geom')||',
g.'||quote_ident('basin')||',
a.'||quote_ident('DATE')||',
a.'||quote_ident('VALUE')||'
FROM sarffg_basins_00_regional_3sec AS g JOIN '||quote_ident(prod_table)||' AS a
ON g.'||quote_ident('basin')||' = a.'||quote_ident('BASIN')||'
WHERE a.'||quote_ident('DATE')||' = '''||adj_timestamp||'''';
I have verified that prod_table has a column called "VALUE", so I'm not sure why I would be seeing this error. It is also important to note that calling my procedure from within psql yields no errors.
(I have two very similar return statements because my code queries a table with capital column names, and in the absence of that table it creates one from a CSV that doesn't have the capital names.)
Also not sure if it's relevant but here is what my function returns:
RETURNS table (
gid integer,
geom geometry(MultiPolygon,4326),
basin double precision,
date timestamptz,
value double precision
)
Any help would be greatly appreciated
I guess, you use the field VALUE in a filter or something similar in the mapfile (hard to say for sure without mapfile).
This filter must expect capitalized column names and this is why the original query had also capitalized column names:
select g.gid, g.geom, g.basin, a.\"DATE\", a.\"VALUE\" from....
If so, you only have to capitalize the columns returned by your procedure:
RETURNS table (
gid integer,
geom geometry(MultiPolygon,4326),
basin double precision,
"DATE" timestamptz,
"VALUE" double precision
)
Remember that in PostgreSql the case of column and table names matter if you surround then with double quote.
This query:
SELECT VALUE from ...
is case independent, while this one:
SELECT "VALUE" from ...
really requires a table with capitalized column names. And tables with capitalized column names require double quote:
CREATE TABLE test ("VALUE" text, .....
Related
I tried to merge a quarterly table and a monthly table by using INNER JOIN LATERAL, but encountered error.
state_smm table, monthly
unemp table, quarterly
my code is:
SELECT st.state,
st.monthly_reporting_period AS month,
st.CPR,
up.unemp_rate AS Most_Recent_Quarterly_unemp
FROM state_smm AS st
INNER JOIN LATERAL (
-- Find the value for latest quarter that ended on or before the monthly date
SELECT unemp.unemp_rate
FROM unemp
WHERE unemp.state = st.property_state
AND unemp.quarter <= st.monthly_reporting_period
ORDER BY unemp.quarter DESC
limit 1
)AS up
and I got error:
ERROR: syntax error at end of input
LINE 14: )AS up
^
SQL state: 42601
Character: 434
How can I edit my code? Thanks a lot.
How to convert below Oracle query to Postgres? Below is the error
ERROR: syntax error at or near "BY"¶ Position: 321
Query
SELECT listagg(app_rule_cd,',') within GROUP (
ORDER BY abc_cd) AS ERR_LST,
'1' AS JOIN1
FROM ABC_RULE
WHERE abc_cd IN
( WITH CTE AS
(SELECT VAL FROM config_server WHERE NAME = 'XXXXXXXXXX'
)
SELECT TRIM(REGEXP_SUBSTR( VAL, '[^,]+', 1, LEVEL))
FROM CTE
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(VAL, '[^,]+')) + 1 // BY is position 321
);
You did not explain what this query does, but the convoluted connect by level and regexp_replace() is a typically pattern to split a comma separated string into elements in Oracle.
That can be done way easier in Postgres:
SELECT string_agg(app_rule_cd,',' ORDER BY abc_cd) AS ERR_LST,
'1' AS JOIN1
FROM ABC_Rule
WHERE abc_cd = ANY ( (SELECT string_to_array(val, ',')
FROM config_server WHERE NAME = 'XXXXXXXXXX') )
Note the duplicated parentheses around the sub-query are necessary. Another way is to use the IN operator:
SELECT string_agg(app_rule_cd,',' ORDER BY abc_cd) AS ERR_LST,
'1' AS JOIN1
FROM ABC_Rule
WHERE abc_cd IN (SELECT unnest(string_to_array(val, ','))
FROM config_server
WHERE NAME = 'XXXXXXXXXX')
I have a query which returns:
amount id
23414 21
234234 21
23434 21
235434 22
3453 22
345345 22
345345 22
I have another query which return:
{10,227,185,22,228,186,21,164}
I link the first query with the second with the following code:
SELECT first_query from first_data
where id in (SELECT array_agg(unnest::int) from (
SELECT unnest(reg) from (
SELECT REGEXP_MATCHES(svals, '(\d+)=>', 'g') as reg
FROM (SELECT svals(additional_infos) as svals from logs bl) as s
)
The error:
operator does not exist: integer = integer[]
Conclusion:
They are both integer and I want to get the rows where the id is IN the second query.
Any contribution would be appreciated.
EDIT:
The addition_infos column has the following type of data in each cell:
"ex_bal"=>"{11=>0.0, 14=>0.0, 263=>0.0}", "leg_bal"=>"{\"local\"=>142800.0, 221=>{\"network\"=>0.0}, 73=>{\"network\"=>1463970.84589323}}"
If your 'Second_Query' returns an array then you can use:
SELECT first_query from first_data WHERE id = ANY(SELECT second_query)
I'm trying to run the following query with postgres's support for LATERAL subqueries:
with s as
(
select
tagValues ->> 'mode' as metric
, array_agg(id) as ids
from
metric_v3.v_series
where
name = 'node_cpu'
group by 1
)
select
t.starttime
, s.metric
, t.max
from
s, lateral (
select
d.starttime
, max(d.max) as max
from
metric_v3.gaugedata d
where
d.starttime >= '2020-01-17T00:00Z' AND d.starttime < '2020-01-24T00:00Z'
and d.seriesid in s.ids
group by 1
) t
order by 1,2;
It fails with, where s relates to the reference in the lateral subquery's where clause.
SQL Error [42601]: ERROR: syntax error at or near "s"
I have tried different methods for the lateral query, but I always get the same error. I don't know what I'm missing.
If I run the CTE expression and select s.* from it, I get the expected results, so that part is working fine.
I'm running Postgres 11.6 on CentOS.
You can't use IN with an array. You need to use the ANY operator:
and d.seriesid = any(s.ids)
I've written a simple query that uses a WITH clause, but I'm getting this error:
Error : ERROR: missing FROM-clause entry for table "cte"
Here's the query, where I'm clearly putting a FROM clause. I know this must be simple but I'm just not seeing what I've done wrong. Thanks.
WITH cte AS (
SELECT cident, "month"
FROM orders_extended io
WHERE io.ident = 1 -- 1 will be replaced with a function parameter
)
SELECT *
FROM orders_extended o
WHERE o.cident = cte.cident AND o."month" = cte."month"
ORDER BY o."month" DESC, o.cname
The message didn't lie.
WITH cte AS (
SELECT cident, "month"
FROM orders_extended io
WHERE io.ident = 1 -- 1 will be replaced with a function parameter
)
SELECT o.*
FROM orders_extended o
INNER JOIN cte ON (o.cident = cte.cident and o."month" = cte."month")
ORDER BY o."month" DESC, o.cname