I have a schema as show like the below, and I want to run a query where I get a column in the output for every row of the points
table.
So for each usage
row I want to multiply the amount
of the usage times the amount
for the referenced points_id
, and then sum that up and group by person. So for the example data I'd want output that looked like this:
Name | foo | bar | baz
-------|------|------|------
Scott | 10.0 | 24.0 | 0.0
Sam | 0.0 | 0.0 | 46.2
Here's the schema/data:
CREATE TABLE points (
ident int primary key NOT NULL,
abbrev VARCHAR NOT NULL,
amount real NOT NULL
);
CREATE TABLE usage (
ident int primary key NOT NULL,
name VARCHAR NOT NULL,
points_id integer references points (ident),
amount real
);
INSERT INTO points (ident, abbrev, amount) VALUES
(1, 'foo', 1.0),
(2, 'bar', 2.0),
(3, 'baz', 3.0);
INSERT INTO usage (ident, name, points_id, amount) VALUES
(1, 'Scott', 1, 10),
(2, 'Scott', 2, 12),
(3, 'Sam', 3, 3.4),
(4, 'Sam', 3, 12);
I'm using PostgreSQL 9.2.8
The data is just sample. There are thousands of rows in the real usage
table and probably a dozen in the points
table. The real intent here is I don't want to hardcode all the points
summations as I use them in many functions.
SQL Fiddle Example:http://sqlfiddle.com/#!15/cc84a/6;
Use following PostgreSQL function for dynamic cases:
Function uses temporary table to store dynamic columns data.
Call function in following way to get data:
select * from sp_test(); select * from temp_data;