Using SQL, how could I give values to dates, grouped by each company?
Current Table:
Date Company Employees
2012-04-28 Apple, Inc. 7543
2012-04-27 Apple, Inc. 7510
2012-04-26 Apple, Inc. 7484
2012-04-28 Google, Inc. 11303
2012-04-27 Google, Inc. 11300
2012-04-26 Google, Inc. 11280
2012-04-25 Google, Inc. 11278
2012-04-24 Google, Inc. 11254
Desired Table:
Date company_day Company Employees
2012-04-28 3 Apple, Inc. 7543
2012-04-27 2 Apple, Inc. 7510
2012-04-26 1 Apple, Inc. 7484
2012-04-28 5 Google, Inc. 11303
2012-04-27 4 Google, Inc. 11300
2012-04-26 3 Google, Inc. 11280
2012-04-25 2 Google, Inc. 11278
2012-04-24 1 Google, Inc. 11254
The value for company_day begins at the earliest date for each company. company_day is company specific.
I would first get the
MINdate for each company.Then you can
JOINthe result of that query and simply display the difference between it and the Date column.Something like that. This code is untested, but should be close.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
http://www.w3schools.com/sql/func_datediff_mysql.asp