Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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;
share|improve this question
1  
What happened when you tried insert into stats(ave_age,stdv) select avg(age), stddev(age) from T1; Sems to be fine. –  ypercube Jun 17 at 17:29

1 Answer 1

up vote 0 down vote accepted

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
share|improve this answer
    
Thanks all for the responses. Not at my workstation but I will test tonight! –  Jim Miller Jun 17 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 at 0:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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