I’m using WebMatrix to build a webpage that queries our accounting software and displays the top salesman of the day.
I have 4 Salesmen: sam (salesman 7) Jared (salesman 8) Charlie (salesman 12) Mitchell (salesman 17)
Query code:
var ssjb = db.QueryValue("SELECT SUM(SUBTOTAL) FROM dbo.DR_TRANS where Transdate = 41195 and SALESNO = 7 and subtotal >0");
var sjdp = db.QueryValue("SELECT SUM(SUBTOTAL) FROM dbo.DR_TRANS where Transdate = 41195 and SALESNO = 8 and subtotal >0");
var scjb = db.QueryValue("SELECT SUM(SUBTOTAL) FROM dbo.DR_TRANS where Transdate = 41195 and SALESNO = 12 and subtotal >0");
var smms = db.QueryValue("SELECT SUM(SUBTOTAL) FROM dbo.DR_TRANS where Transdate = 41195 and SALESNO = 17 and subtotal >0");
Now, I want to evaluate the results of the four queries, and display “Top Salesman: name“
I’m lost as to how to evaluate the query then turn the salesman number into the corresponding name.
EDIT: Database structure:
- dbo.staff has lots of columns but the first two are
staffno(primary key) andName. - dbo.DR_TRANS contains again many columns but the two we are working with are
subtotalandsalesno– neither of which are keys. This table has a scrolling key which is the invoice number. Salesnois derived fromstaffno(passed through by the app from login details.)
Below, (I think) us the query you are looking for. Both tables are joined using
INNER JOINthrough their Linking column theDR_TRANS.Salesno = Staff.StaffNo. It will list all theStaffwho has record and whichsubtotal > 0. No need to add a condition for thestaffNoas they are listed on descending order based on their total transaction.