I have table effort_sheet as below,
----------------------------------------------- Pid WeekendDate Mon Tue Wed Thr Fri Sat Sun ---------------------------------------------- 11 14.10.2012 4 4 5 1 0 0 0 11 07.10.2012 2 2 1 5 3 0 0 12 07.10.2012 2 2 0 0 0 0 0 Here, WeekendDate is date on 'Sun'(last column)
I need to find the date for each Pid when last non-zero value was logged.
So my result should be something like
Pid LastValueDate 11 11.10.2012 12 02.10.2012 11 -> 11.10.2012 was Thr(value 1) 12 -> 02.10.2012 was Tue(value 2)
As my data is in Excel, an d I am using JDBC-ODBC connection. I would prefer we this could be achieved just with SQL.
My incomplete solution (need to find last_effort_day):
SELECT pid
,max(WeekendDate) - (6-last_effort_day) AS end_date
FROM effort_sheet
GROUP BY pid
Please help.
sqlfiddle link