2013-06-18 73 views
0

我有四個查詢,每個查詢有6列。除WHERE子句在每種情況下略有不同外,每個查詢都是相同的。我希望看到的是eachother旁邊的每個列的每個查詢結果進行比較。示例結果表頭:time(only one),calls1,calls2,calls3,calls4,work1,work2,work3,work4,tele1,tele2,tele3,tele4,comm1,comm2,comm3,comm4,techs1,techs2 ,techs3,techs4。加入四個具有相同列,不同條件的MySQL查詢

實際查詢如下。請幫我做一個比較性的查詢。牛逼

SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS r_calls, ROUND(AVG(work_time),2)/60 AS r_work, ROUND(AVG(tele_time),2)/60 AS r_tele, ROUND(AVG(comm_time),2)/60 AS r_comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS r_techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE call_origins_uid != 5 
AND DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15) 


SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS calls, ROUND(AVG(work_time),2)/60 AS work, ROUND(AVG(tele_time),2)/60 AS tele, ROUND(AVG(comm_time),2)/60 AS comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE call_origins_uid != 5 
AND DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
AND call_origins_uid = 1 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15) 


SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS calls, ROUND(AVG(work_time),2)/60 AS work, ROUND(AVG(tele_time),2)/60 AS tele, ROUND(AVG(comm_time),2)/60 AS comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE call_origins_uid != 5 
AND DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
AND call_origins_uid = 4 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15) 


SELECT CONCAT(hour(opened_dt),':',floor(minute(opened_dt)/15)*15) AS time, COUNT(*)/COUNT(DISTINCT DATE(opened_dt)) AS calls, ROUND(AVG(work_time),2)/60 AS work, ROUND(AVG(tele_time),2)/60 AS tele, ROUND(AVG(comm_time),2)/60 AS comm, IFNULL(COUNT(*)/COUNT(DISTINCT DATE(opened_dt)),0)/3 AS techs 
FROM detail_head LEFT JOIN detail_detail ON detail_detail.detail_head_uid = detail_head.detail_head_uid 
WHERE DATE(opened_dt) >= (CURDATE() - INTERVAL 42 DAY) 
AND dayname(opened_dt) = 'SUNDAY' 
GROUP BY (hour(opened_dt)*100)+floor(minute(opened_dt)/15) 
+0

您需要格式化您的查詢,以便有人爲您提供幫助。這是非常重要的眼睛:/ – user962449

+0

更新了查詢,使他們更容易閱讀。對於那個很抱歉! – brandoncluff

回答

1

你可以使用「CREATE TEMPORARY TABLE TableName1」爲每個表,然後編寫一個查詢把列順序然後刪除該表時,你完成。

+0

你能解釋一下嗎?我之前沒有使用過臨時表。 – brandoncluff

+0

當然,除了關鍵字'臨時'以外,創建表的語法與普通表相同。不同的是該表僅存在於當前連接。另一個選擇是使用您的查詢來創建視圖,然後查詢視圖。 – jksloan

+0

你能舉個例子嗎?看法? – brandoncluff

相關問題