I have three functions that receive no arguments and just return the most recent data from 3 different tables in 3 different schemas. Each function returns 4 columns of data, all of the same type. How do I create 1 function that calls all three functions and gives the output of each function?
I tried using Perform "function1"; Perform "function2"; then select * from function3(); roughly speaking and nothing worked.
Related
What is the most efficient way to apply a binary function to some columns of two table?
For example, let
var1=[1;2;3;4];
var2=[1;1;1;3];
A=table(var1,var2);
var1=[2;3;4;5];
var2=[1;1;1;3];
B=table(var1,var2);
If I want the new table C where
C=table(func(A.var1,B.var1),var2);
with for example func=#mean,
what is the most efficient way to achieve the result? Is [bsxfun(#func,A(:,1),B(:,1),var2)] efficient?
In my application, I need to first remove rows where var2 is 0 and where A.var2 and B.var2 are non-identical. I am using syntax like A=A(A.var2~=0,:) and [~,ia,ic]=intersect(A.var2,B.var2); A=A(ia,:). And func would be a combination of the above two steps and [mean(A(:,1),B(:,1)),var2)] before using bsxfun. However, at that point, I've effectively looped over the columns of each table for 3 times. Is there a more efficient way with built-in functions to apply a binary function to select columns of 2 tables based on some conditions?
I have 4 queries with MIN(), MAX(), AVG(), stddev_pop() functions. But I need a way in which I can generalize these queries into a single query with structure
"param1(param2)".
where param1 can be MIN/MAX/AVG/stddev_pop. And param 2 can be the argument to these functions.
But I am not able to do so.
create a wrapper
function void(function_name, listparam, numberparam)
split params as the number is mentioned (ex:max 1 param)
inside use select case when function='selectedfunction' than call function with splitted params.
With DB2 I'm able to declare anonymous custom types (e.g. row types or composite types) for my user defined functions - see the following example (especially the last line):
DB2 example:
CREATE OR REPLACE FUNCTION myFunction(IN input1 DECIMAL(5), IN input2 DECIMAL(5))
RETURNS DECIMAL(2)
READS SQL DATA
LANGUAGE SQL
NO EXTERNAL ACTION
NOT DETERMINISTIC
BEGIN
DECLARE TYPE customAnonymousType AS ROW(a1 DECIMAL(2), a2 DECIMAL(2), a3 DECIMAL(2));
/* do something fancy... */
Can I do something similar with PL/pgSQL? I know I would be able to use existing row types, also existing user defined types - but do I really have to define the type in advance?
I also know about the RECORD type, but as far as I understand I would not be able to use it in arrays (and also it would not be a well defined type).
Comments asked for an example, even though it does lengthen the question a lot I tried to define a quite simple example (still for DB2):
CREATE OR REPLACE FUNCTION myFunction(IN input1 DECIMAL(5), IN input2 DECIMAL(5))
RETURNS DECIMAL(2)
READS SQL DATA
LANGUAGE SQL
NO EXTERNAL ACTION
NOT DETERMINISTIC
BEGIN
DECLARE TYPE customAnonymousType AS ROW(a1 DECIMAL(2), a2 CHARACTER VARYING(50));
DECLARE TYPE customArray AS customAnonymousType ARRAY[INTEGER];
DECLARE myArray customArray;
SET myArray[input1] = (50, 'Product 1');
SET myArray[input2] = (99, 'Product 2');
RETURN myArray[ARRAY_FIRST(myArray)].a1;
END
This function of course only serves as a dummy function (but I suppose it is already quite long for a question here). Actually it just decides which number to return depending on if input1 is greater than input2. If input1 is smaller than input2, it returns 50, if input2 is smaller or equal than input2 it would return 99.
I know I'm not even using my a2 character field of my type (so in this case I would also be able to just use an number array) and that there are probably many, many better solutions to return two fixed numbers depending on the input values, but still my original questions remains if I am able to use anonymous custom types in PL/pgSQL (as I would in Oracle or DB2 procedures) - or if there are any similar alternatives.
You cannot to create types with local visibility in Postgres. This functionality is not supported. Postgres support global custom composite types only.
See CREATE TYPE doc. This statement cannot be used in DECLARE part of plpgsql block.
I want to create a function in pgsql that will take three parameters and return a single value (1 row, 1 col).
I have the query working but can not seem to find a proper example of creating a function to do this.
Take a look at the example on this page, or this one.
If you actually go through the documentation there are multiple example of various functions and return types.
I am writing a PL/pgSQL function. The function has input parameters which specify (indirectly), which tables to read filtering information from.
The function embeds business logic which allows it to select data from different tables based on the input arguments. The function dynamically builds a subquery which returns filtering data which is then used to run the main query.
My questions are:
Is it 'legal' to use a dynamic subquery in a PL/pgSQL function. I cant see why not - but this question is related to the next one.
AFAIK, PL/pgSQL are cached or precompiled by the query engine. How does having a function that generates dynamic subqueries impact the work of the query engine?
"38.5.4. Executing Dynamic Commands"