分组统计(平均值计算)

时间:2021-02-03 00:44:39

1.以样地号分组求值,cast() as....指转换类型   ,decimal(,)指保留小数点

select 样地号,cast(SUM(平均树高*断面积*样地活株数)/SUM(断面积*样地活株数) as decimal(4,2)) as '平均树高' from Sheet2$ group by 样地号
2.case...when...then...end的用法

select 样地号,优势树种,组成=
case
when 组成 >=1 then cast(FLOOR(组成) AS nvarchar)+优势树种
when 组成<1 and 组成!=0 then cast(1 AS nvarchar)+优势树种
when 组成=0 then CAST(0 as nvarchar)+优势树种
end
from Sheet2$
go