Home > Enterprise >  SQL Server stored procedure to create commission report for partner wise
SQL Server stored procedure to create commission report for partner wise

Time:11-05

We are getting order wise details in a table

order orderDate order value commission partner1 partner2 partner3
1 1-Oct 1000 50 A B C
2 1-Oct 800 40 C D F
3 2-Oct 2000 100 B A K

This commission will be divided into multiple partners based on predefined formula

Need to create daily & monthly earning for each partner so that amount can be given to them based on their earning

PartnerID startdate enddate total earning
A 1-Oct 20-Oct 500
B 1-Oct 20-Oct 300

I need to write a stored procedure in SQL Server to make this daily & monthly earning report partner wise

CodePudding user response:

A short explanation is added to the DBFIDDLE:

select 
   partner,
   sum(commis) as commission
from (
   select partner1 as partner, commission/3 as commis
   from orders
   where orderDate between '2022-10-01' and '2022-10-20'
   union all 
   select partner2, commission/3 as commis
   from orders
   where orderDate between '2022-10-01' and '2022-10-20'
   union all 
   select partner3, commission/3 as commis
   from orders 
   where orderDate between '2022-10-01' and '2022-10-20'
  )c
group by partner;

NOTE:

  • A long explanation is not done here because this is very basic SQL stuff.
  • When calculating commission (commission/3) SQL returns an integer, because two integers are divided. It would be better to divide by 3.0, which will return a decimal value. (But this is left as an exercise for you!)
  • Related