Среднее по группам в SQL - PullRequest
       1

Среднее по группам в SQL

0 голосов
/ 03 октября 2019

У меня есть запрос, и я хочу получить среднее значение для каждого Place_id, но мой запрос возвращает неправильный вывод.

Что-то не так в моем запросе?

SELECT DISTINCT 
    place_id,
    SUM(fc.[main_percent_score]) aVG,
    CASE 
       WHEN fc.[main_percent_score] >= 0.00 AND fc.[main_percent_score] <= 0.99 
          THEN '0'
       WHEN fc.[main_percent_score] > 1.0 AND fc.[main_percent_score] <= 10.0 
          THEN '10'
       WHEN fc.[main_percent_score]  > 10.0 and fc.[main_percent_score]  <= 20.0 then  '20'
    when fc.[main_percent_score]  >20.0 and fc.[main_percent_score]  <= 30.0 then  '30'
    when fc.[main_percent_score]  >30.0 and fc.[main_percent_score]  <= 40.0 then  '40'
    when fc.[main_percent_score]  >40.0 and fc.[main_percent_score]  <= 50.0 then  '50'
    when fc.[main_percent_score]  >50.0 and fc.[main_percent_score]  <= 60.0 then  '60'
    when fc.[main_percent_score]  >60.0 and fc.[main_percent_score]  <= 70.0 then  '70'
    when fc.[main_percent_score]  >70.0 and fc.[main_percent_score]  <= 80.0 then  '80'
    when fc.[main_percent_score]  >80.0 and fc.[main_percent_score]  <= 90.0 then  '90'
    when fc.[main_percent_score]  >90.0 and fc.[main_percent_score]  <= 100.0 then  '100'
    end "PerTest" 
from [final scoring based on main] fc
group by case
  when fc.[main_percent_score]  >= 0.00 and fc.[main_percent_score]  <= 0.99 then  '0'
  when fc.[main_percent_score]  > 1.0 and fc.[main_percent_score]  <= 10.0 then  '10'
  when fc.[main_percent_score]  > 10.0 and fc.[main_percent_score]  <= 20.0 then  '20'
  when fc.[main_percent_score]  >20.0 and fc.[main_percent_score]  <= 30.0 then  '30'
  when fc.[main_percent_score]  >30.0 and fc.[main_percent_score]  <= 40.0 then  '40'
  when fc.[main_percent_score]  >40.0 and fc.[main_percent_score]  <= 50.0 then  '50'
  when fc.[main_percent_score]  >50.0 and fc.[main_percent_score]  <= 60.0 then  '60'
  when fc.[main_percent_score]  >60.0 and fc.[main_percent_score]  <= 70.0 then  '70'
  when fc.[main_percent_score]  >70.0 and fc.[main_percent_score]  <= 80.0 then  '80'
  when fc.[main_percent_score]  >80.0 and fc.[main_percent_score]  <= 90.0 then  '90'
  when fc.[main_percent_score]  >90.0 and fc.[main_percent_score]  <= 100.0 then  '100'
    end,
    place_id
ORDER BY
    place_id

результат этого запроса:

2   10  10
2   20  20

Я ожидаю, что результат должен быть b 30

1 Ответ

0 голосов
/ 03 октября 2019

Вы можете попробовать ниже и увидеть:

select sum(aVG),sum(PerTest),place_id

from (

 select DISTINCT 
  place_id,
  sum(fc.[main_percent_score]) aVG,
  case
    when fc.[main_percent_score]  >= 0.00 and fc.[main_percent_score]  <= 0.99 
      then  '0'
    when fc.[main_percent_score]  > 1.0 and  fc.[main_percent_score]  <= 10.0 then  '10'
    when fc.[main_percent_score]  > 10.0 and fc.[main_percent_score]  <= 20.0 then  '20'
    when fc.[main_percent_score]  >20.0 and fc.[main_percent_score]  <= 30.0 then  '30'
    when fc.[main_percent_score]  >30.0 and fc.[main_percent_score]  <= 40.0 then  '40'
    when fc.[main_percent_score]  >40.0 and fc.[main_percent_score]  <= 50.0 then  '50'
    when fc.[main_percent_score]  >50.0 and fc.[main_percent_score]  <= 60.0 then  '60'
    when fc.[main_percent_score]  >60.0 and fc.[main_percent_score]  <= 70.0 then  '70'
    when fc.[main_percent_score]  >70.0 and fc.[main_percent_score]  <= 80.0 then  '80'
    when fc.[main_percent_score]  >80.0 and fc.[main_percent_score]  <= 90.0 then  '90'
    when fc.[main_percent_score]  >90.0 and fc.[main_percent_score]  <= 100.0 then  '100'
    end "PerTest" 
from [final scoring based on main] fc
group by case
  when fc.[main_percent_score]  >= 0.00 and fc.[main_percent_score]  <= 0.99 then  '0'
  when fc.[main_percent_score]  > 1.0 and fc.[main_percent_score]  <= 10.0 then  '10'
  when fc.[main_percent_score]  > 10.0 and fc.[main_percent_score]  <= 20.0 then  '20'
  when fc.[main_percent_score]  >20.0 and fc.[main_percent_score]  <= 30.0 then  '30'
  when fc.[main_percent_score]  >30.0 and fc.[main_percent_score]  <= 40.0 then  '40'
  when fc.[main_percent_score]  >40.0 and fc.[main_percent_score]  <= 50.0 then  '50'
  when fc.[main_percent_score]  >50.0 and fc.[main_percent_score]  <= 60.0 then  '60'
  when fc.[main_percent_score]  >60.0 and fc.[main_percent_score]  <= 70.0 then  '70'
  when fc.[main_percent_score]  >70.0 and fc.[main_percent_score]  <= 80.0 then  '80'
  when fc.[main_percent_score]  >80.0 and fc.[main_percent_score]  <= 90.0 then  '90'
  when fc.[main_percent_score]  >90.0 and fc.[main_percent_score]  <= 100.0 then  '100'
  end,
  place_id
--order by place_id

) t

group by place_id
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...