MYSQL-扁平表查询

MYSQL-扁平表查询,mysql,sql,pivot,Mysql,Sql,Pivot,我对下面的查询有一些问题,我不确定如何修改它以获得所需的输出,详细内容如下: SELECT listup.NodeNumber As Node, listup.Station As Extension, CASE WHEN VoiceServer = 1 THEN KeyDescription ELSE '' END AS 'Key1', CASE WHEN VoiceServer = 2 THEN KeyDescription ELSE '' END AS 'Key

我对下面的查询有一些问题,我不确定如何修改它以获得所需的输出,详细内容如下:

SELECT listup.NodeNumber As Node, listup.Station As Extension,
       CASE WHEN VoiceServer = 1 THEN KeyDescription ELSE '' END AS 'Key1',
       CASE WHEN VoiceServer = 2 THEN KeyDescription ELSE '' END AS 'Key2',
       CASE WHEN VoiceServer = 3 THEN KeyDescription ELSE '' END AS 'Key3',
       CASE WHEN VoiceServer = 4 THEN KeyDescription ELSE '' END AS 'Key4',
       CASE WHEN VoiceServer = 5 THEN KeyDescription ELSE '' END AS 'Key5',
       CASE WHEN VoiceServer = 6 THEN KeyDescription ELSE '' END AS 'Key6',
       CASE WHEN VoiceServer = 7 THEN KeyDescription ELSE '' END AS 'Key7',
       CASE WHEN VoiceServer = 8 THEN KeyDescription ELSE '' END AS 'Key8',
       CASE WHEN VoiceServer = 9 THEN KeyDescription ELSE '' END AS 'Key9',
       CASE WHEN VoiceServer = 10 THEN KeyDescription ELSE '' END AS 'Key10'
  FROM listup
ORDER BY listup.NodeNumber, listup.Station;
结果如下:

Node    Extension  Key1   Key2   Key3  etc.
N100    14311   14311                                    
N100    14311       14308                                
N100    14311           14309                            
N100    14311               14314                        
N100    14311                   14412                    
N100    14311                       14535                
N100    14311                           14316            
N100    14311                               14456        
N100    14312   14312                                    
N100    14312       14442                                
N100    14312           14311                            
N100    14312               14314                        
N100    14312                   14456                    
N100    14312                       14309                
N100    14312                           14308            
我想让它全部显示在一行上,如此扁平。。 例如


您需要定义一个
groupby
子句,并使用
MAX
聚合来获得所需的输出:

  SELECT listup.NodeNumber As Node, listup.Station As Extension,
         MAX(CASE WHEN VoiceServer = 1 THEN KeyDescription ELSE NULL END) AS 'Key1',
         MAX(CASE WHEN VoiceServer = 2 THEN KeyDescription ELSE NULL END) AS 'Key2',
         MAX(CASE WHEN VoiceServer = 3 THEN KeyDescription ELSE NULL END) AS 'Key3',
         MAX(CASE WHEN VoiceServer = 4 THEN KeyDescription ELSE NULL END) AS 'Key4',
         MAX(CASE WHEN VoiceServer = 5 THEN KeyDescription ELSE NULL END) AS 'Key5',
         MAX(CASE WHEN VoiceServer = 6 THEN KeyDescription ELSE NULL END) AS 'Key6',
         MAX(CASE WHEN VoiceServer = 7 THEN KeyDescription ELSE NULL END) AS 'Key7',
         MAX(CASE WHEN VoiceServer = 8 THEN KeyDescription ELSE NULL END) AS 'Key8',
         MAX(CASE WHEN VoiceServer = 9 THEN KeyDescription ELSE NULL END) AS 'Key9',
         MAX(CASE WHEN VoiceServer = 10 THEN KeyDescription ELSE NULL END) AS 'Key10'
    FROM listup
GROUP BY listup.NodeNumber As Node, listup.Station As Extension
ORDER BY listup.NodeNumber, listup.Station

NULL
比长度为零的字符串更可取。

@Syeda:称呼和陈词滥调毫无价值;浪费屏幕不动产。天哪,小马-言语无法表达我现在有多爱你!这么长时间以来,我一直在疯狂地试图让它发挥作用!英雄@威尔:你可以看到你离得有多近:)
  SELECT listup.NodeNumber As Node, listup.Station As Extension,
         MAX(CASE WHEN VoiceServer = 1 THEN KeyDescription ELSE NULL END) AS 'Key1',
         MAX(CASE WHEN VoiceServer = 2 THEN KeyDescription ELSE NULL END) AS 'Key2',
         MAX(CASE WHEN VoiceServer = 3 THEN KeyDescription ELSE NULL END) AS 'Key3',
         MAX(CASE WHEN VoiceServer = 4 THEN KeyDescription ELSE NULL END) AS 'Key4',
         MAX(CASE WHEN VoiceServer = 5 THEN KeyDescription ELSE NULL END) AS 'Key5',
         MAX(CASE WHEN VoiceServer = 6 THEN KeyDescription ELSE NULL END) AS 'Key6',
         MAX(CASE WHEN VoiceServer = 7 THEN KeyDescription ELSE NULL END) AS 'Key7',
         MAX(CASE WHEN VoiceServer = 8 THEN KeyDescription ELSE NULL END) AS 'Key8',
         MAX(CASE WHEN VoiceServer = 9 THEN KeyDescription ELSE NULL END) AS 'Key9',
         MAX(CASE WHEN VoiceServer = 10 THEN KeyDescription ELSE NULL END) AS 'Key10'
    FROM listup
GROUP BY listup.NodeNumber As Node, listup.Station As Extension
ORDER BY listup.NodeNumber, listup.Station