sql - select top 10 with the highest average score -


lets say, have product , score tables.

product ------- id name  score ----- id productid scorevalue 

i want top 10 products highest average scores, how average , select top 10 products in 1 select statement?

here mine selects unexpected rows

select top 10 product.productname score.score  product, score  product.id  in (select top 100  productid                          score                         group productid                         order sum(score) desc)  order score.score desc 

give try,

with records (     select  a.id, a.name, avg(b.scorevalue) avg_score,             dense_rank() on (order avg(b.scorevalue) desc) rn        product             inner join score b                 on a.id = b.productid     group   a.id, a.name ) select  id, name, avg_score    records   rn <= 10 order   avg_score desc 

the reason why not using top because not handle duplicate record having highest average. can use top ties instead.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -