Reputation: 319
I have a table like below
table1
---------
x y z total average
========================
1 2 3
2 3 4
3 4 5
here i need what ever am selecting columns in front end corresponding columns only need a total and average for example if i select x,y,total,average columns in front end i need a output like below
x y total avg
1 2 3 1.5
2 3 5 2.5
3 4 7 3.5
here am using refcursor with dynamicsql
create or replace function sample_refcursor(i_column in varchar) returns refcursor as
$$
declare
c1 refcursor;
begin
drop table if exists temp_t;
create temp table temp_t as select x as A,y as B,z as C,total as tot,avg as average from table1;
open c1 for execute('select ' ||i_column|| ' from temp_t group by ' ||i_column);
return c1;
close c1;
end;
$$ language plpgsql
Upvotes: 0
Views: 42
Reputation: 8839
SELECT x, y, x+y AS tot, (x+y)/2 AS Avg From TableName
I as per your second table you are not using z.
Use below code
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=publishing user=www password=foo")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = 'SELECT x, y, x+y AS tot, (x+y)/2 AS Avg From TableName';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
?>
Upvotes: 1
Reputation: 3848
You may try: SELECT x,y, x+y AS total, (x+y)/2 AS average FROM Table;
Upvotes: 1