2017-02-27 16 views
1

我編寫了用於提取數據的獨立查詢。現在我想要在一個查詢中加入所有查詢。如何加入?如何在一個查詢中組合六個獨立的查詢。 TABLE IS SINGLE但條件不同

1. 
select Name, COUNT(name) as Total_Complaint, 
    count(Solved) as SC, COUNT(Pending)as PC 
from helpdesk 
WHERE Solved="yes" OR Pending="yes" 
group by name; 

2. 
SELECT name,count(name)as Total_Compalint,count(feedback), 
    count(feedback)/count(name)*100 as feed_Percent 
from helpdesk 
group by NAME; 

3. 
select name, sum(feedback)/(count(feedback)*5)*5 as AVG_Feedback 
from helpdesk 
group by name; 

4. 
select Name, COUNT(name) as Total_Complaint, 
    count(Solved) as SC, COUNT(Pending)as PC 
from helpdesk 
group by name; 

5. 
SELECT name, 
    sum(TIMESTAMPDIFF(MINUTE,Request_Time, Close_Time))as Working_Time, 
    540-sum(TIMESTAMPDIFF(MINUTE,Request_Time, Close_Time))as VC 
from helpdesk 
group by name ; 

6. 
select name, 
    concat(count(case when etr_meet = 'yes' then 1 else null end) * 100/count(1), '%') 
from `helpdesk` 
group by name; 
+0

你不把它們組合起來。你爲什麼? –

+0

我想從一個表中獲取數據,但條件不同 –

+0

但我的要求是在一個表中顯示所有結果 –

回答

3

由於您的所有查詢都處於相同模式 - select ... from helpdesk group by name將它們合併到一個查詢中很有意義。

請注意,使用CASE語句將第1個查詢(WHERE Solved="yes" OR Pending="yes")中的WHERE子句移入COUNT

select  Name 
      ,COUNT(case when Solved="yes" OR Pending="yes" then name end) as Total_Complaint 
      ,count(Solved)             as SC 
      ,COUNT(Pending)             as PC     
      ,count(name)              as Total_Compalint 
      ,count(feedback) 
      ,count(feedback)/count(name)*100         as feed_Percent 
      ,sum(feedback)/(count(feedback)*5)*5        as AVG_Feedback 
      ,sum(TIMESTAMPDIFF(MINUTE,Request_Time, Close_Time))    as Working_Time 
      ,540-sum(TIMESTAMPDIFF(MINUTE,Request_Time, Close_Time))   as VC   
      ,concat(count(case when etr_meet = 'yes' then 1 else null end) * 100/count(1), '%') 

from  helpdesk 

group by name 
; 
相關問題