I have a table with Transactions, amongst whose columns are id
, created_at
, and company_id
. I'd like to group the four first transactions of every company and return the created_at values of each transaction on each row.
In other words, I want each row of my output to correspond to the four first transactions of each company (so grouping by company_id
) with columns showing me the company_id
and the created_at of each of those four transactions.
How do I do that?
Sample data:
id | company_id | created_at
---------------------------------
1123 | abcd | 10/12/2015
8291 | abcd | 10/14/2015
9012 | abcd | 10/15/2015
9540 | abcd | 10/16/2015
10342 | abcd | 10/21/2015
10456 | abcd | 10/22/2015
2301 | efgh | 10/13/2015
4000 | efgh | 11/01/2015
4023 | efgh | 11/03/2015
6239 | efgh | 11/08/2015
7500 | efgh | 11/14/2015
Sample output:
company_id | created_at_1 | created_at_2 | created_at_3 | created_at_4
--------------------------------------------------------------------------
abcd | 10/12/2015 | 10/14/2015 | 10/15/2015 | 10/16/2015
efgh | 10/13/2015 | 11/01/2015 | 11/03/2015 | 11/08/2015