誰かが助けてくれるなら、喜びもなくさまざまなコンボを試して、一日中これに苦労していますか? WITH ROLLUP を使用して合計列を合計し、各列の合計を出力するテーブルの足に行を追加します。
ここで、最後の列 [利益] を降順で並べる必要があるため、最大数が一番上になります。以下のクエリを使用してこれを達成しましたが、ROLLUP 行もソートします。ロールアップを別のエンティティとして持つ方法はありますか?
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
どんな助けでも感謝します。
G