表 T_price 用于记录商品的价格 PID 是外关键字对应商品表
Price 价格
DTime 日期id PID Price DTime1 3 0.88 2006-8-1
2 2 0.80 2006-8-1
3 4 0.60 2006-8-2......
现在需要选择 一周内 平均价最高的10个商品 和 平均价最低的10个商品请问怎么用最简单的方法查询到 谢谢
Price 价格
DTime 日期id PID Price DTime1 3 0.88 2006-8-1
2 2 0.80 2006-8-1
3 4 0.60 2006-8-2......
现在需要选择 一周内 平均价最高的10个商品 和 平均价最低的10个商品请问怎么用最简单的方法查询到 谢谢
where DTime=时间范围
group by PID
order by avg(price)
select top 10 PID,avg(price) from table
where DTime=时间范围
group by PID
order by avg(price) desc
from
(
select PID,avg(Price) as Pricefrom T_pricewhere DTime>dateadd(wk,-1,getdate())group by PID
) t
order by Price desc