一個人得到10%的轉介,以便他的推薦朋友進行購買。mysql查詢生成基於被推薦成員的調查報告
有兩個表:
- 參考表
- 事務表
參考表
Person_id Referrer_id
3 1
4 1
5 1
6 2
事務表
Person_id Amount Action Date
3 100 Purchase 10-20-2011
4 200 Purchase 10-21-2011
6 400 Purchase 12-15-2011
3 200 Purchase 12-30-2011
1 50 Commision 01-01-2012
1 10 Cm_Bonus 01-01-2012
2 20 Commision 01-01-2012
如何得到以下結果集爲Referrer_Person_id = 1
Month Ref_Pur Earn_Comm Todate_Earn_Comm BonusRecvd Paid Due
10-2011 300 30 30 0 0 30
11-2011 0 0 30 0 0 30
12-2011 200 20 50 0 0 50
01-2012 0 0 50 10 50 0
上面使用的標籤是:
Ref_Pur = Total Referred Friend's Purchase for that month
Earn_Comm = 10% Commision earned for that month
Todate_Earn_Comm = Total Running Commision earned upto that month
MySQL的代碼,我寫
SELECT dx1.month,
dx1.ref_pur,
dx1.earn_comm,
(@cum_earn := @cum_earn + dx1.earn_comm) as todate_earn_comm
FROM
(
select date_format(`date`,'%Y-%m') as month,
sum(amount) as ref_pur ,
(sum(amount)*0.1) as earn_comm
from transaction tr, reference rf
where tr.person_id=rf.person_id and
tr.action='Purchase' and
rf.referrer_id=1
group by date_format(`date`,'%Y-%m')
order by date_format(`date`,'%Y-%m')
)as dx1
JOIN (select @cum_earn:=0)e;
如何加入 該查詢還包括BonusRecvd,Paid和Due trnsactions,它不依賴於參考表?
,並生成行月'11 -2011' ,即使沒有發生trnx上月
@chris我試過的一個可以顯示ref_pur,Earn_comm,Todate_Earn_Comm,但不能包括BonusRecvd,付費和到期。 –
@chris因爲當月沒有trnx發生,所以'11 -2011'月也無法生成行 –
你能發佈你的代碼嗎? –