2016-04-23 57 views
-1

在表格和羣組上加入2個表格並進行多項查詢請幫助我的學校營銷項目。我正在尋找整天,並嘗試我的代碼工作,我也嘗試了一些在這裏stackoverflow的答案,但我不能得到正確的結果。請看我上傳的表格圖片。
TABLES image here please click如何通過


SELECT d.user_ID, d.weekof, 
TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(d.total_time))), '%H:%i') AS st_time, SUM(d.twcm) AS stwcm, SUM(d.slas) AS sslas, SUM(d.bas) AS sbas, SUM(d.iph) AS siph, SUM(d.ias) AS sias, 
SUM(IF(d.lead_type = 'expireds', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS expireds, 
SUM(IF(d.lead_type = 'fsbos', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS fsbos, 
SUM(IF(d.lead_type = 'followup', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS followup, 
SUM(IF(d.lead_type = 'buyers', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS buyers, 
SUM(IF(d.lead_type = 'agents', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS agents, 
SUM(IF(d.lead_type = 'circlemarketing', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS circlemarketing, 
SUM(IF(d.lead_type = 'doorknocking', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS doorknocking, 
SUM(IF(d.lead_type = 'investor', d.twcm + d.slas + d.bas + d.iph + d.ias, 0)) AS investors, 
w.user_ID, w.weekof, SUM(w.slah) AS sslah, SUM(w.bah) AS sbah, SUM(w.sls) AS ssls, SUM(w.bls) AS sbls, SUM(w.nuc) AS snuc 
FROM daily_table d 
JOIN weekly_table w ON d.user_ID = w.user_ID 
WHERE d.user_ID = 'kien' 
GROUP BY weekof 

新的更新的代碼,但結果總和()是不正確的總和。

SELECT w.user_ID, w.weekof, SUM(w.slah) AS sslah, SUM(w.bah) AS sbah, SUM(w.sls) AS ssls, SUM(w.bls) AS sbls, SUM(w.nuc) AS snuc, 
d.user_ID, TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(d.total_time))), '%H:%i') AS st_time, SUM(d.twcm) AS stwcm, SUM(d.slas) AS sslas, 
SUM(d.bas) AS sbas, SUM(d.iph) AS siph, SUM(d.ias) AS sias, 
SUM(IF(d.lead_type = 'expireds', twcm + slas + bas + iph + ias, 0)) AS expireds, 
SUM(IF(d.lead_type = 'fsbos', twcm + slas + bas + iph + ias, 0)) AS fsbos, 
SUM(IF(d.lead_type = 'followup', twcm + slas + bas + iph + ias, 0)) AS followup, 
SUM(IF(d.lead_type = 'buyers', twcm + slas + bas + iph + ias, 0)) AS buyers, 
SUM(IF(d.lead_type = 'agents', twcm + slas + bas + iph + ias, 0)) AS agents, 
SUM(IF(d.lead_type = 'circlemarketing', twcm + slas + bas + iph + ias, 0)) AS circlemarketing, 
SUM(IF(d.lead_type = 'doorknocking', twcm + slas + bas + iph + ias, 0)) AS doorknocking, 
SUM(IF(d.lead_type = 'investor', twcm + slas + bas + iph + ias, 0)) AS investors 
FROM wp_weekly_goals w LEFT JOIN wp_daily_goals d ON d.weekof=w.weekof GROUP BY weekof 
+0

我有更新代碼在這裏,但結果總和(0數字是不正確的 –

回答

0

我在別人面前盡了全力。我得到了正確的結果。謝謝。

SELECT w.*, d.* FROM 
(SELECT user_ID, weekof, SUM(slah) AS sslah, SUM(bah) AS sbah, SUM(sls) AS ssls, SUM(bls) AS sbls, SUM(nuc) AS snuc FROM weekly_table WHERE user_ID='kien' GROUP BY weekof) w 
LEFT JOIN 
(SELECT user_ID, weekof, TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(total_time))), '%H:%i') AS st_time, SUM(twcm) AS stwcm, SUM(slas) AS sslas, 
SUM(bas) AS sbas, SUM(iph) AS siph, SUM(ias) AS sias, 
SUM(IF(lead_type = 'expireds', twcm + slas + bas + iph + ias, 0)) AS expireds, 
SUM(IF(lead_type = 'fsbos', twcm + slas + bas + iph + ias, 0)) AS fsbos, 
SUM(IF(lead_type = 'followup', twcm + slas + bas + iph + ias, 0)) AS followup, 
SUM(IF(lead_type = 'buyers', twcm + slas + bas + iph + ias, 0)) AS buyers, 
SUM(IF(lead_type = 'agents', twcm + slas + bas + iph + ias, 0)) AS agents, 
SUM(IF(lead_type = 'circlemarketing', twcm + slas + bas + iph + ias, 0)) AS circlemarketing, 
SUM(IF(lead_type = 'doorknocking', twcm + slas + bas + iph + ias, 0)) AS doorknocking, 
SUM(IF(lead_type = 'investor', twcm + slas + bas + iph + ias, 0)) AS investors 
FROM daily_table WHERE user_ID='kien' GROUP BY weekof) d ON d.weekof=w.weekof AND d.user_ID=w.user_ID