I have a table of data containing time log information.
create table "time_records" (
"id" serial NOT NULL PRIMARY KEY,
"start" timestamp not null,
"end" timestamp not null,
"duration" double precision not null,
"project" varchar(255) not null,
"case" integer not null,
"title" text not null,
"user" varchar(255) not null
);
Here's a few lines of data:
"id","start","end","duration","project","case","title","user"
"1","2014-02-01 11:54:00","2014-02-01 12:20:00","26.18","Project A","933","Something done here","John Smith"
"2","2014-02-02 12:34:00","2014-02-02 15:00:00","146","Project B","990","Something else done","Joshua Kehn"
"3","2014-02-02 17:57:00","2014-02-02 18:39:00","41.38","Project A","933","Another thing done","Bob Frank"
"4","2014-02-03 09:30:00","2014-02-03 11:41:00","131","Project A","983","iOS work","Joshua Kehn"
"5","2014-02-03 10:22:00","2014-02-03 13:29:00","187.7","Project C","966","Created views for things","Alice Swiss"
I can pull bits and pieces of information out of this. For example, a list of every project with time logged between two dates or every person that worked between two dates.
What I would like is to be able to generate a report with the date and then each project across the top with the total amount of time logged for that project.
SELECT
start::date,
sum(duration / 60) as "time logged",
project
FROM
time_records
WHERE
project = 'Project A'
GROUP BY
start::date, project
ORDER BY
start::date, project;
However I want multiple columns on the output, so combining a select distinct project
with this in some way.
Final output would be something like:
date, project a total, project b total, project c total,
2014-02-01,0.5, 0.3, 10,
2014-02-02,1.3, 20, 3,
2014-02-03,20, 10, 10
...
I can get the total per-date per-project with something like:
SELECT
start::date,
sum(duration / 60) as "time logged",
project
FROM
time_records
GROUP BY
start::date, project
ORDER BY
start::date, project;
But then I have multiple dates in rows per-project. I need it to be one date with per-project totals on separate lines.
Does this make sense / is possible with just SQL not writing some code after the query?