0

Postgresql 9.3, Mac OS X 10.6.7

I created this function and I would like to insert both results into the same row. Obviously the way I have it inserts into 2 rows.

First, here is the Table T1:

 test_app_development=# select * from T1;
   name  | age | id
  -------+-----+----
   jim   |  50 |  1
  bill   |  55 |  2
   Ed    |  35 |  3
   joe   |  30 |  4
  dave   |  45 |  5
  steve  |  38 |  6
   (6 rows)

Now the Function:

  create or replace function stats_test() returns numeric as $$
    begin
     insert into stats(stdv) select stddev(age) from T1;
     insert into stats(ave_age) select avg(age) from T1;
     return 1;
    end;
 $$ language plpgsql;

And here are the results to the function:

 select stats_test();

 test_app_development=# select * from stats;
    ave_age | id | stdv 
   ---------+----+-------
            |  1 | 9.496
    42.167  |  2 |     
   (2 rows)

I have tried variations but with no luck:

  insert into stats(ave_age,stdv) select avg(age), stddev(age) from T1;
1
  • 1
    What happened when you tried insert into stats(ave_age,stdv) select avg(age), stddev(age) from T1; Sems to be fine. Jun 17, 2014 at 17:29

1 Answer 1

0

I have tried it with this function, it works fine for me.

create or replace function stats_test() returns numeric as $$
  begin
    insert into stats(ave_age, stdv) select avg(age), stddev(age) from "T1";
    return 1;
  end;
$$ language plpgsql
2
  • Thanks all for the responses. Not at my workstation but I will test tonight!
    – Jim Miller
    Jun 17, 2014 at 19:14
  • Ah, I'm stupid. I looked at the logs and I saw I did it this way: 'insert into stats(stdv) stats(ave_age) select stddev(age), avg(age) from T1;' Must've been sleep deprived! Thanks so much for the help!
    – Jim Miller
    Jun 18, 2014 at 0:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.