--统计出日销售量
select p.name,sum_num(od.num)
into #t
from product p
join orderdetail od on p.id=od.Productid
join orders o on o.id=od.Orderid
where o.yn=0 --假设0代表没有删除
and a.createtime=getdate() --统计今天--生成排名
select name,sum_num,排名=(
select count(distinct sum_num) from #t
where sum_num>=a.sum_num)
from #t a
order by 排名--处理完成后删除临时表
drop table #t

解决方案 »

  1.   

    --统计出日销售量
    select p.name,sum_num=sum(od.num)  --**** 上面这里写错了 ****
    into #t
    from product p
    join orderdetail od on p.id=od.Productid
    join orders o on o.id=od.Orderid
    where o.yn=0 --假设0代表没有删除
    and a.createtime=getdate() --统计今天--生成排名
    select name,sum_num,排名=(
    select count(distinct sum_num) from #t
    where sum_num>=a.sum_num)
    from #t a
    order by 排名--处理完成后删除临时表
    drop table #t
      

  2.   

    --如果不用临时表select name,sum_num,排名=(
    select count(distinct sum_num) 
    from (
    select p.name,sum_num=sum(od.num)
    from product p
    join orderdetail od on p.id=od.Productid
    join orders o on o.id=od.Orderid
    where o.yn=0 --假设0代表没有删除
    and a.createtime=getdate() --统计今天
    )aa where aa.sum_num>=a.sum_num)
    from(
    select p.name,sum_num=sum(od.num)
    from product p
    join orderdetail od on p.id=od.Productid
    join orders o on o.id=od.Orderid
    where o.yn=0 --假设0代表没有删除
    and a.createtime=getdate() --统计今天
    )a order by 排名