I have a table in the format
Date | Name | Result
---------------------------
2012-05-02 | bob | W
2012-04-01 | kevin | D
2012-03-21 | mark | L
2012-03-19 | bob | W
and would like to produce a query that i can then export to excel. at the moment the table contains some 3000 rows and a name may appear anywhere between once and 60 times and there are well 893 different unique names in the data currently.
I would like the format to be where the columns contain results ordered by the date column ascending, i.e. R1 is the oldest result for that name, R2 the second oldest. the final column (Rn) would be the most recent result. and easy way to see streaks etc.
Name | R1 | R2 | R3 ....
-------------------
Bob | W | W | D | W ....
Mark | D | L |
Trevor |
I can do this for an individual name by selecting name, date result ordering by date ascending but this is returned as columns which of course i can manually edit in excel to columns, however is there a way to do this in sql for each name?
something like:
select f_name,
select f_result from table a where f_name=b.f_name order by f_date asc limit 1 as r1,
select f_result from table a where f_name=b.f_name order by f_date asc limit 2,1 as r2,
select f_result from table a where f_name=b.f_name order by f_date asc limit 3,1 as r3
from table b, table b
where a.id=b.id
but obviously programtically as this would be quite time consuming to type, update and maintain.
kind regards
edit: i should note that i have rights to create tables or views etc if creating something that can then be kept up to data via a cron job would be easier.
and then my export was a select * from…
as per my question created a base table, added the columns than ran 52 line of update scripts to insert the values limited to 0,1 up to 51,1 to get the right result.
performance was an issue on my server, so best to group these into 10/15 queries at a time.