I am trying to create a query in mysql to assign row numbers based on multiple columns. Following is the format in which I need the desired results
CN PN GroupName WeekTimeReported rank desired_rank
X ProjX A 12/30/2013 1 1
X ProjX B 12/30/2013 2 1
X ProjX C 1/6/2014 3 2
X ProjX D 1/6/2014 4 2
Y ProjY A 1/13/2014 5 1
Y ProjY B 1/13/2014 6 1
Y ProjY C 1/20/2014 7 2
Y ProjY D 1/20/2014 8 2
Z ProjZ A 1/27/2014 9 1
Z ProjZ B 1/27/2014 10 1
Z ProjZ C 2/3/2014 11 2
Z ProjZ D 2/3/2014 12 2
I want my result in the desired_rank column format. So for Same CN and PN I want to rank based on the WeekTimeReported (I want to see the no of weeks project went for). Thank you for your help in advance.
Here is the SQLFiddle and query:
SELECT
@cn:=ClientName ClientName
,ProjectName
,GroupName
,CAST(WeekTimeReported AS DATE) WeekTimeReported
,@rc:= CASE WHEN @cn=ClientName THEN @rc+1 ELSE 1 END AS rc
,desired_rank
,Hours
FROM sampledata , (select @rc:=0, @cn='') as rc
window functions- making this and similar needs so much easier to achieve