1

我有一张看起来像的桌子

#Sector  max1   avg1   max2  avg2  numb
C         133    14     45    3     27
N         174     9     77    3     18
M         63      3     28    1     16

我想将 N 行和 M 行连接在一起,将其称为 X 并取 max1 和 max2 的最大值,同时在各自的列中取 avg1、avg2 和 numb 的平均值以返回

#Sector  max1   avg1   max2  avg2  numb
C         133    14     45    3     27
X         174     6     77    2     17
4

2 回答 2

3

试试这个方法:

select sector, max1,avg1,max2,avg2,numb
from tab
where sector not in ('M','N')
union all
select 'X' as sector, max(max1),avg(avg1),max(max2),avg(avg2),avg(numb)
from tab
where sector in ('M','N')
于 2013-07-23T19:52:07.040 回答
2

就像是:

select 
  case when sector in ('N','M') then 'X' else sector end sect,
  max(max1) max1,
  avg(avg1) avg1,
  max(max2) max2,
  avg(avg2) avg2,
  avg(numb) numb
from tabname 
group by 
  case when sector in ('N','M') then 'X' else sector end
于 2013-07-23T19:54:35.300 回答