1
Answer

Get Division wise top customers - Sales Report

Kavitha M

Kavitha M

8y
449
1
Hi , 


I have sales report which has Last year sales,This year sales with doller value.I have four parameters as
follows Invoice from date,Invoice to date, Division(Multiple selection) and Top Number of customers(int).
 Now I return top number of cusotmers for each division. For example if i give 10, then it returns top 10 customers within selected division's customers (total 10 records)


my query is like following :

select top (@TopNoCustomer) a.div,a.custID,a.TY_sales from 

(select a.div,a.custID,a.TY_Sales from Table_A a where a.Date between (@Fromdate and @Todate)
and a.div in (@Division)) A Join


(select b.div,b.custID,b.LY_Sales from Table_A b where b.Date between (DATEADD(year, -1, @Fromdate) and DATEADD(year, -1, @Todate))
and a.div in (@Division)) B On A.id = B.id where A.division = B.division
group by a.div , a.custID, a.TY_Sales order by a.TY_Sales desc


Now I want to get the top number of customers for each division. For example if i give 10 then it should return top 10 customer for each selected division (if 4 division selected,then it should return 40 records)


Can anyone give me idea to modify my SQL query to return division wise customer
Thanks in advance



Answers (1)