简体   繁体   中英

Is it possible to pass an array into a PostgreSQL function and perform a SELECT on all instances of the array

I'm trying to create a function in PostgreSQL to count some values based on three parameters ( a start date, an end date and a team's uuid ) that is passed into the function.

I want to know if it is possible to pass an array of team uuid's into this function and perform a SELECT count for all of these teams.

My code:

CREATE OR REPLACE FUNCTION funcSelectedTeamActivtyDashboard
     (IN aStartDate date, IN aEndDate date, IN aTeamID uuid[])
RETURNS TABLE(teamName text, a integer, b integer, c integer, d integer) 
AS
$BODY$

SELECT t.name AS teamName, tm.name AS memberName,
    count(distinct a._id_) FILTER (WHERE a.datetime_registered BETWEEN aStartDate AND aEndDate)::integer AS a,
    count(distinct b._id_) FILTER (WHERE b.datetime_completed BETWEEN aStartDate AND aEndDate)::integer AS b,
    count(distinct c._id_) FILTER (WHERE c.datetime_completed BETWEEN aStartDate AND aEndDate)::integer AS c,
    count(distinct d._id_) FILTER (WHERE d.start_date BETWEEN aStartDate AND aEndDate)::integer AS d
FROM team_member tm
JOIN team t ON ...
JOIN table_a a ON ...
LEFT JOIN table_b b ON ...
LEFT JOIN table_c c ON ...
JOIN table_d d ON ...

WHERE t._id_ = ANY(aTeamID)

$BODY$
  LANGUAGE sql VOLATILE
  COST 100
  ROWS 1000;

I want to use this function to say pass in either:

A) aTeamID = abc123 and then perform these counts for this selected team and over the selected dates. It should return something like...

- TEAM1  MEMBER1     2   4   5   2
- TEAM1  MEMBER2     1   5   1   3
- TEAM1  MEMBER3     3   3   2   2
- TEAM1  MEMBER4     7   9   7   3

OR

B) aTeamID = abc123, def456, ghi789 and then perform these counts for all of these teams and over the selected dates. It should return something like...

- TEAM1  MEMBER1     2   4   5   2
- TEAM1  MEMBER2     1   5   1   3
- TEAM1  MEMBER3     3   3   2   2
- TEAM1  MEMBER4     7   9   7   3
- TEAM2  MEMBER1     2   4   5   2
- TEAM2  MEMBER2     1   5   1   3
- TEAM2  MEMBER3     3   3   2   2
- TEAM2  MEMBER4     7   9   7   3
- TEAM3  MEMBER1     2   4   5   2
- TEAM3  MEMBER2     1   5   1   3
- TEAM3  MEMBER3     3   3   2   2
- TEAM3  MEMBER4     7   9   7   3

I have left out some basic parts of my query as they are obvious as to what it is and should do... My focus is on being able to pass in an array to the function and then count for all of the instances in the array.

I do have a basic knowledge of PostgreSQL and SQL but I haven't come across anything like this before and I'm not sure if it is even possible. Thanks in advance for any help provided and please feel free to ask for more details should it be necessary.

Speaking specifically of ANY, yes you can.

ANY expects array expression to work , like

SELECT 'Apple' = ANY(ARRAY['Apple', 'Orange', 'Mango']);

So in your case its input would be something like ARRAY['abc123', 'def456', 'ghi789'].

You can also is "IN" in place of ANY. Its simple as

SELECT 'Apple' IN ('Apple', 'Orange', 'Mango');

Further more these could help.

IN vs ANY operator in PostgreSQL

postgreSQL - in vs any

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM