SQL Server 2000 数据库表结构如下theTypeName goodsNum price
酱油 12 33
麻油 3 21
大米 67 4.5
大米 80 4.5
洗洁精 150 1.5如果对于同一物品存在多个相同的采购进仓价格,那么只取一条,就相当于 select distinct 的效果,要求:1. 只有1个价格参考 ,直接取
2. 只有2个价格参考 ,取平均数
3. 只有3个价格参考 ,取中间价格
4. 有3个价格以上参考 ,剔除最高最低,然后中间所有价格计算平均数
酱油 12 33
麻油 3 21
大米 67 4.5
大米 80 4.5
洗洁精 150 1.5如果对于同一物品存在多个相同的采购进仓价格,那么只取一条,就相当于 select distinct 的效果,要求:1. 只有1个价格参考 ,直接取
2. 只有2个价格参考 ,取平均数
3. 只有3个价格参考 ,取中间价格
4. 有3个价格以上参考 ,剔除最高最低,然后中间所有价格计算平均数
if object_id('[tb]') is not null drop table [tb]
create table [tb]([theTypeName] varchar(6),[goodsNum] int,[price] numeric(3,1))
insert [tb]
select '酱油',12,33 union all
select '麻油',3,21 union all
select '大米',67,7.5 union all
select '大米',80,4.5 union all
select '大米',80,5.5 union all
select '洗洁精',150,1.5select [theTypeName],
case count(1) when 1 then max([price]) when 2 then avg([price])
else (select avg([price]) from [tb] where [theTypeName]=t.[theTypeName] and [price]<>
(select max([price]) from [tb] where [theTypeName]=t.[theTypeName]) and [price]<>
(select min([price]) from [tb] where [theTypeName]=t.[theTypeName]))
end as [price]
from [tb] t
group by [theTypeName]
------------------------
大米 5.500000
酱油 33.000000
麻油 21.000000
洗洁精 1.500000
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([theTypeName] varchar(6),[goodsNum] int,[price] numeric(3,1))
insert [tb]
select '酱油',12,33 union all
select '麻油',3,21 union all
select '大米',67,7.5 union all
select '大米',80,4.5 union all
select '大米',80,5.5 union all
select '洗洁精',150,1.5select [theTypeName],
case count(1) when 1 then max([price]) when 2 then avg([price])
else (select avg([price]) from [tb] where [theTypeName]=t.[theTypeName] and [price]<>
max(t.[price]) and [price]<>
min(t.[price]))
end as [price]
from [tb] t
group by [theTypeName]
-----------------------
大米 5.500000
酱油 33.000000
麻油 21.000000
洗洁精 1.500000