been struggling with this all day now trying various combos without any joy if anyone can help? I use WITH ROLLUP to sum a Total column which adds a row to foot of table outputting each columns sum.
I now need to order the last column [profits] descending so highest number is at top. I have achieved this using query below but it also sorts the ROLLUP row, is there a method to have the rollup as a separate entity so the ORDER BY is only done on normal records?
Select
*
From
(Select
c.ContactFullName As Adviser,
Sum(If(Month(b.CaseDate) = 1, b.LeadCost, 0)) As Jan,
Sum(If(Month(b.CaseDate) = 2, b.LeadCost, 0)) As Feb,
Sum(If(Month(b.CaseDate) = 3, b.LeadCost, 0)) As Mar,
Sum(If(Month(b.CaseDate) = 4, b.LeadCost, 0)) As Apr,
Sum(If(Month(b.CaseDate) = 5, b.LeadCost, 0)) As May,
Sum(If(Month(b.CaseDate) = 6, b.LeadCost, 0)) As Jun,
Sum(If(Month(b.CaseDate) = 7, b.LeadCost, 0)) As Jul,
Sum(If(Month(b.CaseDate) = 8, b.LeadCost, 0)) As Aug,
Sum(If(Month(b.CaseDate) = 9, b.LeadCost, 0)) As Sep,
Sum(If(Month(b.CaseDate) = 10, b.LeadCost, 0)) As Oct,
Sum(If(Month(b.CaseDate) = 11, b.LeadCost, 0)) As Nov,
Sum(If(Month(b.CaseDate) = 12, b.LeadCost, 0)) As Decb,
Sum(b.LeadCost) As LeadCosts,
Sum(b.CaseCommission) As GrossComm,
(Sum(b.CaseCommission) * 40 / 100) As GHL_Comm,
(Sum(b.CaseCommission) * 40 / 100 - Sum(b.LeadCost)) As Profit
From
tblcontacts a Inner Join
tblcases b On a.ContactID = b.ContactID Inner Join
mi_tblcontacts c On c.Mi_ContactID = b.ContactAssignedTo Inner Join
tblreferral d On d.RefferalID = a.ContactReferrelSource
Group By
c.ContactFullName With Rollup) q
Order By
q.Profit Desc
Any help appreciated.
G
You could try something like this: