Inside a function there is a simple single table query:
create function myfunction(...)
LANGUAGE 'plpgsql'
...
return query select 1, variable1, xtcol1 from xtable where xtcolx=variablex;
...
when running it select myfunction(...) got
ERROR: column reference "xtcol1" is ambiguous
LINE 1: select 1, variable1, xtcol1 from xtable ...
^
DETAIL: It could refer to either a PL/pgSQL variable or a table column.
QUERY: ...
CONTEXT: PL/pgSQL function myfunction(integer,character varying) line 20 at RETURN QUERY
SQL state: 42702
Surely I can fix it by prefix every column in the query, but it makes no sense because I named all columns with a unique table abbreviation. For example xtable abbreviation is xt so there is no ambiguous columns across the board. It works out well in all other functions except this. Postgre v12
The correct way is to qualify the ambiguous name with either the table (alias) or the function name:
SELECT xtable.xtcol1 FROM xtable
so error message :
DETAIL: It could refer to either a PL/pgSQL variable or a table column.
shows you are passing a parameter with the same name as your column,
so how about you escape it :
select 1, variable1, "xtcol1" from xtable ...
I want to know how I can enter a parameter within a function type date since at the time of doing so, the following error
report_soat(date, date)
Undefined function: 7 ERROR: operator does not exist: date >= character varying LINE 6: BETWEEN fecha_in AND fecha_out ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. QUERY: SELECT soats.status AS "estado", soats.placa AS "placa" FROM soats where soats.task_id IS NOT NULL and date(soats.created) BETWEEN fecha_in AND fecha_out CONTEXT: PL/pgSQL function report_soat(character varying,character varying) line 2 at RETURN QUERY
SELECT
soats.status AS "estado",
soats.placa AS "placa"
FROM soats
where soats.task_id IS NOT NULL and date(soats.created)
BETWEEN start_date AND end_date;
IN start_date date, IN end_date date, OUT status varchar, OUT placa
varchar
Try...
report_soat('2017-11-14'::date, '2017-11-14'::date)
I want to calculate the average number from a column in PostgreSQL
SELECT AVG(col_name)
From TableName
It gives me this error:
ERROR: function avg (character varying) does not exist
LINE 1: SELECT AVG(col_name)
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Store numbers in a numeric field, an integer, decimal or whatever. But not in a text/varchar field.
Check the manual for all numeric data types.
Nasty workaound: CAST some records to a numeric value and keep others as text. Example:
/*
create temp table foo AS
SELECT x FROM (VALUES('x'),('1'),('2')) sub(x);
*/
WITH cte AS (
SELECT
CASE
WHEN x ~ '[0-9]' THEN CAST(x AS decimal) -- cast to numeric field
END AS num,
CASE
WHEN x ~ '[a-zA-Z]' THEN x
END AS a
FROM foo
)
SELECT AVG(num), COUNT(a) FROM cte;
I have created some types and then a table that has these types:
Create Type Data_typ AS (
... );
Create Type Communication_typ As (
...;
Create Type CreditCard_typ As (
...);
Create Type Name_typ As (
...);
Create Type Personal_typ As (
...);
Create Type Address_typ As (
...);
Create Type Customers_typ As (
CustomerID integer,
Data Data_typ,
Communication Communication_typ,
CreditCard CreditCard_typ,
Name Name_typ,
Personal Personal_typ,
Address Address_typ);
Create Table Customers_1 of Customers_typ(
primary key (CustomerID));
Also, I have another table, named customers that has some data in it. What I want to do is to create a function that will copy all the elements from customers into customers_1. The columns of customers are the same as customers_1,but on customers_1 I have created types that include some of the columns.
('Customers' has 20 columns, and I have broken that into the 6 types that are on Customers_1).
Here is my function:
Create OR REPLACE Function CopyCustomers() RETURNS VOID AS
$$
Begin
Insert Into Customers_1(
Select
NEW Data_typ (username, password),
new communication_typ(email,phone),
new creditCard_typ(creditcardtyp,creditcard,creditcardexpiration),
new name_typ(firstname,lastname),
new personal(age,income,gender),
new address(address1,address2,coty,state,zip)
From Customers);
End;
$$ LANGUAGE plpgsql;
I'm not sure if the new is correct. The error I get is
ERROR: syntax error at or near "("
LINE 7: NEW Data_typ (username, password),
^
********** Error **********
ERROR: syntax error at or near "("
SQL state: 42601
Character: 119
Update - I tried using the ROW syntax:
Create OR REPLACE Function CopyCustomers() RETURNS VOID AS
$$
Begin
Insert Into Customers_1
Select
ROW(username, password),
ROW(email,phone),
ROW(creditcardtyp,creditcard,creditcardexpiration),
ROW(firstname,lastname),
ROW(age,income,gender),
ROW(address1,address2,coty,state,zip)
From Customers;
End;
$$ LANGUAGE plpgsql;
I execute the Function, and I get an error:
ERROR: cannot cast type record to data_typ
LINE 4: ROW(username, "Password"),
^
QUERY: Insert Into Customers_1
Select
CustomerID,
ROW(username, password),
ROW(email,phone),
ROW(creditcardcype,creditcard,creditcardexpiration),
ROW(firstname,lastname),
ROW(age,income,gender),
ROW(address1,address2,city,state,zip)
From CustomerS
CONTEXT: PL/pgSQL function copycustomers() line 3 at SQL statement
********** Error **********
ERROR: cannot cast type record to data_typ
SQL state: 42846
Context: PL/pgSQL function copycustomers() line 3 at SQL statement
Couple of problems.
Insert Into Customers_1(
Select
You don't need the paren there, it's just
INSERT INTO customers_1 SELECT ....
As for this:
NEW Data_typ (username, password),
are you just trying to create a Data_typ, where Data_typ is a composite type created with CREATE TYPE Data_typ AS (...) ?
If so, you want:
ROW(username, password)::xy
example, given CREATE TYPE xy AS (x integer, y integer);:
regress=> SELECT new xy(1,2);
ERROR: syntax error at or near "("
LINE 1: SELECT new xy(1,2);
^
regress=> SELECT xy(1,2);
ERROR: function xy(integer, integer) does not exist
LINE 1: SELECT xy(1,2);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
regress=> SELECT ROW(1,2)::xy;
row
-------
(1,2)
(1 row)
I want to calculate the average number from a column in PostgreSQL
SELECT AVG(col_name)
From TableName
It gives me this error:
ERROR: function avg (character varying) does not exist
LINE 1: SELECT AVG(col_name)
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Store numbers in a numeric field, an integer, decimal or whatever. But not in a text/varchar field.
Check the manual for all numeric data types.
Nasty workaound: CAST some records to a numeric value and keep others as text. Example:
/*
create temp table foo AS
SELECT x FROM (VALUES('x'),('1'),('2')) sub(x);
*/
WITH cte AS (
SELECT
CASE
WHEN x ~ '[0-9]' THEN CAST(x AS decimal) -- cast to numeric field
END AS num,
CASE
WHEN x ~ '[a-zA-Z]' THEN x
END AS a
FROM foo
)
SELECT AVG(num), COUNT(a) FROM cte;