-1

我有一个查询的输出:

IIS7        afsgateway  sharepoint  drupal      j2eeapp     Others      frontpage
----------- ----------- ----------- ----------- ----------- ----------- -----------
5637        3691        2321        518         212         107         30

我想获得最大值及其列名,我的意思是,IIS75637,但我不知道如何实现它。请问有什么帮助吗?

4

1 回答 1

1

像这样试试

SELECT MAX(col1),MAX(col2)
FROM (
  SELECT iis7 + ISNULL(iis7dfs,0) + ISNULL(iis7dfs2,0) AS col1,'IIS7' AS col2
  FROM dbo.WebDirectoryTypeStatistics
UNION AL
  SELECT afsgateway, sharepoint, ISNULL(drupal,0) as col1, 'drupal' AS col2
  FROM dbo.WebDirectoryTypeStatistics
UNION ALL
  SELECT j2eeapp, ntgateway + ISNULL(sp2013,0) + ISNULL(spcommunity,0) AS col1, 
             'Others' AS col2
  FROM dbo.WebDirectoryTypeStatistics
UNION ALL
  SELECT frontpage AS col1, 'frontpage' AS col2  
  FROM dbo.WebDirectoryTypeStatistics
) AS temp

我只是重新安排了您的查询,以便将值作为两列返回。col1col2col1包含所有最大值并col2包含相应的列名。

于 2013-09-23T08:47:55.820 回答