1
Hey guys I am stuck at a problem in MySQL, can you please help me.

我有一个表格,可以给某些参数打分。现在我想做的是我想显示一个表格,其中对于每个 LocationID,所有参数值都应该显示在一行中。
这是我的第一个名为 interviewmaster 的表,用于存储记录

InterviewID     InterviewDate           IntervieweeID  SourceID   LocationID 
1           2013-06-10 16:11:09              1            1        3    
2           2013-06-10 16:12:19              2            2        2    
3           2013-06-10 16:13:05              3            5        5    
4           2013-06-10 16:13:46              4            4        6    
5           2013-06-10 16:14:35              5            2        1    
6           2013-06-10 16:15:26              6            3        7    
7           2013-06-10 16:16:05              7            5        8    
8           2013-06-10 16:16:51              8            1        4    
9           2013-06-10 16:22:31              9            5        3    
10          2013-06-11 08:51:20             10            1        1    

我的第二张表是 interviewparameters

ParameterID   Parameter
1            The Welcome
2            Quality Of Service
3            Speed Of Service
4            Staff Attitude
5            Quality of food/beverage
6            Overall value for money
7            Ambience
8            Overall Experience

我的第三张桌子是面试

InterviewID parameterID Rating
1                  1    5
1                  2    4
1                  3    5
1                  4    4
1                  5    5
1                  6    4
1                  7    5
2                  1    5
2                  2    5
2                  3    5
2                  4    5
2                  5    5
2                  6    4
2                  7    5
3                  1    4
3                  2    3
3                  3    4
3                  4    3
3                  5    4
3                  6    3
3                  7    4
4                  1    2
4                  2    2
4                  3    2
4                  4    2
4                  5    2
4                  6    2
4                  7    2

现在我使用的查询是

select LocationID,
(CASE when ParameterID=1  then avg(interviewratings.Rating) else null end) as Welcome,
(CASE when ParameterID=2  then avg(interviewratings.Rating) else null end) as Service,
(CASE when ParameterID=3  then avg(interviewratings.Rating) else null end) as Speed,
(CASE when ParameterID=4  then avg(interviewratings.Rating) else null end) as Quality,
(CASE when ParameterID=5  then avg(interviewratings.Rating) else null end) as Overall,
(CASE when ParameterID=6  then avg(interviewratings.Rating) else null end) as Ambience,
(CASE when ParameterID=7  then avg(interviewratings.Rating) else null end) as Experience
from interviewratings join interviewmaster on
interviewratings.InterviewID=interviewmaster.InterviewID 
group by ParameterID, LocationID 

这给出了结果

LocationID    welcome   source   speed   quality   overall   Ambiance   experience
1     2.8333                         
2     3.0000                        
3     3.4000                        
4     2.5000                        
5     2.3333                        
6     2.8000                        
7     2.8182                        
8     4.0000                        
1             3.0000                    
2             2.5714                    
3             3.2000                    
4             3.3333                    
5             2.6667                    
6             2.8000                    
7             2.3636                    
8             4.3333                    
1                     3.0000                
2                     2.5714                
3                     3.4000                
4                     2.8333                
5                     3.8889                
6                     2.8000                
7                     2.7273                
8                     3.3333                
1                              3.0000           
2                              4.2857           
3                              3.2000           
4                              3.5000           
5                              2.7778           
6                              2.8000           
7                              3.0000           
8                              3.0000           
1                                        3.0000     
2                                        2.8571     
3                                        3.4000     
4                                        2.8333     
5                                        3.6667     
6                                        2.8000     
7                                        3.1818     
8                                        2.6667     
1                                                 3.0000    
2                                                 2.4286    
3                                                 3.2000    
4                                                 2.8333    
5                                                 2.6667    
6                                                 3.6000    
7                                                 3.0909    
8                                                 3.0000    
1                                                        3.0000
2                                                        2.8571
3                                                        3.8000
4                                                        2.5000
5                                                        2.6667
6                                                        2.8000
7                                                        3.0909
8                                                        3.3333

位置表是

LocationID  Location
1      Seasonal Tastes
2      Daily Treats
3      Eest
4      Prego
5      Mix
6      Splash
7      Xiao chi
8      The Living Room

我希望我的数据是这样的

Location  Welcome  Service  Speed   Quality Overall Ambience  Experience
 seasonal     2.8333   3.0000   3.0000  3.0000  3.0000  3.0000    3.0000            
 Daily treats 3.0000   2.5714   2.5714  4.2857  2.8571  2.4286    2.8571            
 Eest         3.4000   3.2000   3.4000  3.2000  3.4000  3.2000    3.8000           
 Prego        2.5000   3.3333   2.8333  3.5000  2.8333  2.8333    2.5000                        
 Mix          2.3333   2.6667   3.8889  2.7778  3.6667  2.6667    2.6667            
 Splash       2.8000   2.8000   2.8000  2.8000  2.8000  3.6000    2.8000                  
 Xiao chi     2.8182   2.3636   2.7273  3.0000  3.1818  3.0909    3.0909               
 The Living   4.0000   4.3333   3.3333  3.0000  2.6667  3.0000    3.3333               

以及这 8 行中所有列中的所有结果。你能告诉我该怎么做吗?

4

2 回答 2

1

尝试这个:

select 
  l.Location,
  sum(CASE when r.ParameterID=1  then r.Rating else 0 end)/sum(CASE when r.ParameterID=1  then 1 else 0 end) as Welcome,
  sum(CASE when r.ParameterID=2  then r.Rating else 0 end)/sum(CASE when r.ParameterID=2  then 1 else 0 end) as Service,
  sum(CASE when r.ParameterID=3  then r.Rating else 0 end)/sum(CASE when r.ParameterID=3  then 1 else 0 end) as Speed,
  sum(CASE when r.ParameterID=4  then r.Rating else 0 end)/sum(CASE when r.ParameterID=4  then 1 else 0 end) as Quality,
  sum(CASE when r.ParameterID=5  then r.Rating else 0 end)/sum(CASE when r.ParameterID=5  then 1 else 0 end) as Overall,
  sum(CASE when r.ParameterID=6  then r.Rating else 0 end)/sum(CASE when r.ParameterID=6  then 1 else 0 end) as Ambience,
  sum(CASE when r.ParameterID=7  then r.Rating else 0 end)/sum(CASE when r.ParameterID=7  then 1 else 0 end) as Experience
from 
  interviewratings r
join interviewmaster m
  on m.InterviewID = r.InterviewID 
join Location l
  on l.LocationID = m.LocationID 
group by  
  m.LocationID 
于 2013-06-18T07:27:06.457 回答
1

尝试

SELECT l.Location,
       AVG(CASE WHEN r.ParameterID=1 THEN r.Rating ELSE NULL END) Welcome,
       AVG(CASE WHEN r.ParameterID=2 THEN r.Rating ELSE NULL END) Service,
       AVG(CASE WHEN r.ParameterID=3 THEN r.Rating ELSE NULL END) Speed,
       AVG(CASE WHEN r.ParameterID=4 THEN r.Rating ELSE NULL END) Quality,
       AVG(CASE WHEN r.ParameterID=5 THEN r.Rating ELSE NULL END) Overall,
       AVG(CASE WHEN r.ParameterID=6 THEN r.Rating ELSE NULL END) Ambience,
       AVG(CASE WHEN r.ParameterID=7 THEN r.Rating ELSE NULL END) Experience
  FROM interviewmaster i JOIN Location l 
    ON i.LocationID = l.LocationID JOIN interviewratings r
    ON i.InterviewID = r.InterviewID
 GROUP BY i.LocationID, l.Location

这是SQLFiddle演示

于 2013-06-18T07:32:10.260 回答