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.
Related
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.
I'm trying to understand window functions a bit better, and I'm stumped as to why I can't run a nested aggregate function normally, but I can when using a window function.
This is the dbfiddle I'm working off of: https://dbfiddle.uk/?rdbms=postgres_11&fiddle=76d62fcf4066053db18783e70269438c
Before running the window function, basically everything else in my query is evaluated (JOIN and GROUP BY).
So I believe the data the window function is working off of is something like this (after grouping):
Or is it something like this?
So why can I do this: SUM(COUNT(votes.option_id)) OVER(), but I can't do it without OVER()?
As far as I understand, OVER() makes the SUM(COUNT(votes.option_id)) run on this related data set, but it's still a nested aggregate function.
What am I missing?
Thank you very much!
If you have something like SUM(COUNT(votes.option_id)) OVER() you can think of COUNT(votes.option_id) as a column generated in the GROUP BY clause.
According to the documentation:
The rows considered by a window function are those of the “virtual table” produced by the query's FROM clause as filtered by its WHERE, GROUP BY, and HAVING clauses if any.
This means that window functions operate at a level above the GROUP BY clause and any aggregates, and therefore aggregates are available to be used inside window functions. In your example the "virtual table" corresponds to the second picture.
The reason you cannot nest aggregate functions is that you cannot have multiple levels of GROUP BY on the same query. Similarly you cannot nest window functions. The documentation is clear on what type of expression are allowed inside aggregate and window functions. For aggregates functions we can use:
any value expression that does not itself contain an aggregate expression or a window function call
while for window functions we can use:
any value expression that does not itself contain window function calls
I have created a function, now i want to use that function in my query. I have two in parameters to pass in the function. How can i use that query in my function
Is it possible using JPA to have a custom aggregation function that would extend concat() so that it concatenates a column values into a single string?
I faced the same problem recently with JPA and H2 database. I tried to call the GROUP_CONCAT function from JPQL with a function invocation (see below) with no success. Finally, I used a native query call instead.
However, the JPA 2.1 specifications mention that you can call (function invocation) a predefined or user-defined database function either scalar or aggregate one. I have reproduced thereafter the paragraph from the specfication by highlighting the relevant part.
4.6.17.3 Invocation of Predefined and User-defined Database Functions
The invocation of functions other than the built-in functions of the Java Persistence query language is supported by means of the function_invocation syntax. This includes the invocation of predefined database functions and user-defined database functions.
function_invocation::= FUNCTION(function_name {, function_arg}*)
function_arg ::=
literal |
state_valued_path_expression |
input_parameter |
scalar_expression
The function_name argument is a string that denotes the database function that is to be invoked. The arguments must be suitable for the database function that is to be invoked. The result of the function must be suitable for the invocation context.
The function may be a database-defined function or a user-defined function. The function may be a scalar function or an aggregate function.
Applications that use the function_invocation syntax will not be portable across databases.
Example:
SELECT c
FROM Customer c
WHERE FUNCTION(‘hasGoodCredit’, c.balance, c.creditLimit)
Reference: Java Persistence 2.1, Final Release
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.