小弟刚学习SQL不久,用的是sql server 2005,现在有个稍微复杂的问题,不知道如何最简单的写出查询,还请诸位帮忙 :)有一个表T, 列: A, B, P, 其中P是价格,现在我想要计算如下:对T中每一个不同的(A,B)组合,找出所有该组合对应的P集合,并找出其中第90%位高的价格,也就是90%的集合中的价格都低于他。这里面涉及到循环每一个(A,B)组合,还有就是找90%的价格,两个难点我不太会多谢了。

解决方案 »

  1.   

    select a,b,ROW_NUMBER() OVER(PARTITION BY a,b order p desc) as pos into #temp from table order by a,b,p descselect x.a,x.b.x.p from #temp x,(select a,b,cpos=CEILING(max(pos)*0.9) from #temp group by a,b) y where x.a=y.a and x.b=y,b and x.pos=y.cpos
      

  2.   

    select a,b,ROW_NUMBER() OVER(PARTITION BY a+b order p desc) as pos into #temp from table order by a,b,p descselect x.a,x.b.x.p from #temp x,(select a,b,cpos=CEILING(max(pos)*0.9) from #temp group by a,b) y where x.a=y.a and x.b=y,b and x.pos=y.cpos
      

  3.   

    多谢jyxhz,请问你贴的两个有什么区别?一个是PARTITION BY a,b,一个是PARTITION BY a+b给分完毕