oracle - Select First Row of Every Group in sql -
this question has answer here:
- fetch row has max value column 31 answers
i have 2 tables .
1-> sm_employee
(1) employeeid (2) roleid (3) storeid
2-> sm_salesrepworkflow
(1) workflowid (2) salesrepid foreign key employeeid (3) quantityassigned (4) quantityleft (5) month (6) year
by these tables need select first row of every salesrep details sm_salesrepworkflow order salesrepid currentmonth , 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 want
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 many workflow salesrep. want first 1 every salesrep current month , year.
you can use row_number()
function this:
select * from(select workflowid, salesrepid, quantityassigned, quantityleft, month, year , row_number() on (partition salesrepid order workflowid) rownumber sm_salesrepworkflow) rownumber = 1;
Comments
Post a Comment