1

Here is my script, once i run the script, it return in single colomn with value like (val1,val2,val3,bla..bla bla...

CREATE OR REPLACE FUNCTION meter_latest_read_custom()
RETURNS TABLE(
  maxdate timestamp without time zone, 
  ertu integer, 
  meter integer, 
  meter_name character varying, 
  acq_9010 numeric) 
AS
$BODY$
DECLARE
  formal_table text;
BEGIN
  FOR formal_table IN
    SELECT 
      quote_ident(table_name)
    FROM
      information_schema.tables
    WHERE  
      table_schema = 'public' AND
      table_name LIKE 'task%_1'
   LOOP
      RETURN QUERY EXECUTE
      'with groupedft as (
        SELECT meter_id, MAX(acq_date) AS MaxDateTime
          FROM ' ||formal_table|| ' GROUP BY meter_id),
      foo as (
        SELECT 
          ft.acq_date AS maxdate, 
          ft.ertu_id AS ertu, 
          ft.meter_id AS meter, 
          ft.acq_9010 AS acq_9010
        FROM 
          '||formal_table|| ' ft
        INNER JOIN groupedft 
        ON 
          ft.meter_id = groupedft.meter_id 
          AND ft.acq_date = groupedft.MaxDateTime)
      SELECT 
        maxdate, ertu, meter, m.meter_name, acq_9010
      FROM
        foo
      LEFT JOIN 
        meter_record m 
      ON 
        foo.meter=m.meter_id 
        AND foo.ertu=m.ertu_id';

   END LOOP;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION meter_latest_read_custom() OWNER TO postgres;

The result will be return in single column

"("2017-02-16 10:45:00",201,6,"SPARE 6",)"
"("2017-02-16 10:45:00",201,14,"SPARE 14",)"
"("2017-02-16 10:45:00",201,8,"SPARE 8",)"
"("2017-02-16 10:45:00",201,12,"SPARE 12",)"
"("2017-02-16 10:45:00",201,1,"E.CO-PUAS KAJANG/AC PANEL ETS",16986.00000)"
"("2017-02-16 10:45:00",201,2,"SPARE 2",)"
"("2017-02-16 10:45:00",201,3,"SPARE 3",)"
"("2017-02-16 10:45:00",201,10,"SPARE 10",)"
"("2017-02-16 10:45:00",201,11,"SPARE 11",)"
"("2017-02-16 10:45:00",201,4,"SPARE 4",)"

Im required the result return in seperated column not in one column.. where to modify it. ?

-------------------------------------------------------------------
| maxdate   |    ertu   |   meter    |   meter_name |   acq_9010  |
-------------------------------------------------------------------
|           |           |            |              |             |
|           |           |            |              |             |
|           |           |            |              |             |
|           |           |            |              |             |
|           |           |            |              |             |
|           |           |            |              |             |
|           |           |            |              |             |
|           |           |            |              |             |
4
  • Kajang, Malaysian? hi Commented Feb 16, 2017 at 3:25
  • hi bro can u help me... how to separate the column using function Commented Feb 16, 2017 at 4:03
  • Can no problem, but i'm debugging myself could you show the output column as well? It would be easier. Commented Feb 16, 2017 at 4:05
  • @FreedomPride Hi, i already update my post... can u check what i wanted.. Commented Feb 16, 2017 at 4:41

1 Answer 1

3

Try this way

SELECT * FROM meter_latest_read_custom();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.