Select First Row of Every Group in sql [duplicate]

I have two tables .

1-> SM_Employee

 (1) employeeid   
 (2) roleid
 (3) storeid

2-> SM_SalesRepWorkflow

 (1) workflowid
 (2) Salesrepid   foreign key to employeeid
 (3) QuantityAssigned
 (4) QuantityLeft
 (5) month 
 (6) year

By these tables I need to select first row of every SalesRep Details from SM_SalesRepWorkflow order by SalesRepId for CurrentMonth and CurrentYear.

Example

Workflowid SalesRepId QuantityAssigned QuantityLeft Month Year

WF_101 : EMP_101 : 100 : 90 : May : 2013
WF_101 : EMP_102 : 100 : 100 : May : 2013
WF_101 : EMP_103 : 100 : 80 : May : 2013
WF_102 : EMP_101 : 100 : 70 : May : 2013

So result i want is

WF_101 : EMP_101 : 100 : 90 : May : 2013
WF_101 : EMP_102 : 100 : 100 : May : 2013
WF_101 : EMP_103 : 100 : 80 : May : 2013

So There can be many Workflow for a SalesRep. But i want the first one for every SalesRep for current month and year.


You can use the ROW_NUMBER() function like this:

SELECT *
  FROM(SELECT workflowid, salesRepId, quantityAssigned,
              quantityLeft, month, year
              , ROW_NUMBER()
                OVER (PARTITION BY salesRepId
                          ORDER BY workflowid) AS rownumber
         FROM sm_salesRepWorkflow)
 WHERE rownumber = 1;

Fiddle Demo