Postgresql - Stuck building a plpgsql function

Egidi

I am a beginner in plpgsql and i am stuck coding a function. I need a function that does the following:

Giving a table, a id_field in that table and another field in that table it does:

ORIGINAL TABLE

id_field     field_traspose
---------    --------------
    1              A
    1              B
    1              C
    2              A
    3              F
    3              X

RESULT

id_field     field_traspose
---------    --------------
    1              A, B, C
    2              A
    3              F, X

My attempt:

CREATE OR REPLACE FUNCTION traspose(mytable character varying, id_field character varying, field_traspose character varying)
  RETURNS setof RECORD AS
$BODY$ 
DECLARE
    r record;
    result record;
BEGIN

FOR r IN EXECUTE 'SELECT '||id_field||','||field_traspose||'  from '||mytable LOOP

-- Here should go the logic that joins every field_traspose for a same id_field and
--returns the record as one of the returning records (can be many different id_fields)   

RETURN NEXT result;
END LOOP;

RETURN;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

I'm stuck at this point. Regards,

a_horse_with_no_name

No need for such a function, this is already built-in:

select id_field, 
       string_agg(field_traspose, ', ' order by field_traspose) 
from the_table 
group by id_field
order by id_field;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Postgresql function stuck in for loop

From Dev

Postgresql query in plpgsql function slows down after several runs

From Dev

Transaction inside a plpgsql function

From Dev

FOR loop on PLpgSQL function result

From Dev

PLPGSQL Function to Calculate Bearing

From Dev

Eclipse stuck building project

From Dev

Postgresql (PLPGSQL) trigger assignment issue

From Dev

PLPGSQL function returning trigger AND value

From Dev

Unable to execute the function in plpgsql/postgres

From Dev

PLPGSQL function returning trigger AND value

From Dev

Postgresql: execute update on a temporary table in plpgsql is not working

From Dev

In plpgsql (of PostgreSQL), can a CTE be preserved to an outer loop?

From Dev

How to print code, type of PostgreSQL / plpgsql exception

From Dev

plpgsql function return another function value

From Dev

Building PostgreSQL driver for Qt

From Dev

Variables for identifiers inside IF EXISTS in a plpgsql function

From Dev

Declare "LANGUAGE plpgsql" before or after the function body?

From Dev

how to get current row value in plpgsql function

From Dev

How to run plpgsql without creating a function?

From Dev

Call plpgsql function in select's where clause

From Dev

the use of quote_ident() in a plpgsql function

From Dev

How to pass table rows to a plpgsql function?

From Dev

Returning set of rows from plpgsql function.

From Dev

plpgsql function to search and move index in array

From Dev

Return multiple rows from plpgsql function

From Dev

Take in a Java ResultSet the return from a plpgsql function

From Dev

the use of quote_ident() in a plpgsql function

From Dev

How to pass table rows to a plpgsql function?

From Dev

plpgsql function creates sequences for row based tenancy?

Related Related

  1. 1

    Postgresql function stuck in for loop

  2. 2

    Postgresql query in plpgsql function slows down after several runs

  3. 3

    Transaction inside a plpgsql function

  4. 4

    FOR loop on PLpgSQL function result

  5. 5

    PLPGSQL Function to Calculate Bearing

  6. 6

    Eclipse stuck building project

  7. 7

    Postgresql (PLPGSQL) trigger assignment issue

  8. 8

    PLPGSQL function returning trigger AND value

  9. 9

    Unable to execute the function in plpgsql/postgres

  10. 10

    PLPGSQL function returning trigger AND value

  11. 11

    Postgresql: execute update on a temporary table in plpgsql is not working

  12. 12

    In plpgsql (of PostgreSQL), can a CTE be preserved to an outer loop?

  13. 13

    How to print code, type of PostgreSQL / plpgsql exception

  14. 14

    plpgsql function return another function value

  15. 15

    Building PostgreSQL driver for Qt

  16. 16

    Variables for identifiers inside IF EXISTS in a plpgsql function

  17. 17

    Declare "LANGUAGE plpgsql" before or after the function body?

  18. 18

    how to get current row value in plpgsql function

  19. 19

    How to run plpgsql without creating a function?

  20. 20

    Call plpgsql function in select's where clause

  21. 21

    the use of quote_ident() in a plpgsql function

  22. 22

    How to pass table rows to a plpgsql function?

  23. 23

    Returning set of rows from plpgsql function.

  24. 24

    plpgsql function to search and move index in array

  25. 25

    Return multiple rows from plpgsql function

  26. 26

    Take in a Java ResultSet the return from a plpgsql function

  27. 27

    the use of quote_ident() in a plpgsql function

  28. 28

    How to pass table rows to a plpgsql function?

  29. 29

    plpgsql function creates sequences for row based tenancy?

HotTag

Archive