2016-01-07 4 views
0

Я пытаюсь создать гистограмму репутации людей, которые задают вопросы на сайте с помощью StackExchange data explorer.SQL for bucketing counts

следующее выдает сообщение об ошибке:

Each GROUP BY expression must contain at least one column that 
is not an outer reference. 
Invalid column name 'lt_100'. ... 

Предложения оценили

select 
    case when Reputation < 100 then "lt_100" 
     when Reputation >= 100 and Reputation < 200 then "100_199" 
     when Reputation >= 200 and Reputation < 300 then "200_299" 
     when Reputation >= 300 and Reputation < 400 then "300_399" 
     when Reputation >= 400 and Reputation < 500 then "400_499" 
     when Reputation >= 500 and Reputation < 600 then "500_599" 
     when Reputation >= 600 and Reputation < 700 then "600_699" 
     when Reputation >= 700 and Reputation < 800 then "700_799" 
     when Reputation >= 800 and Reputation < 900 then "800_899" 
     when Reputation >= 900 and Reputation < 1000 then "900_999" 
     else "over 1000" 
    end ReputationRange, 
    count(*) as TotalWithinRange 
FROM Users 
JOIN Posts ON Users.Id = Posts.OwnerUserId 
JOIN PostTags ON PostTags.PostId = Posts.Id 
JOIN Tags on Tags.Id = PostTags.TagId 
WHERE PostTypeId = 1 and Posts.CreationDate > '9/1/2010' 
Group by 
1 
+3

Что значит «не работает» означает? Кроме того, пометьте свой вопрос в базе данных, которую вы используете. –

+0

Извините, я добавил сообщение об ошибке. База данных - это исследователь данных StackOverflow (как указано в вопросе). –

ответ

2

Вы должны использовать single-quotes для диапазонов классифицируемого. Если вы используете " ", это будет рассматриваться как имя столбца. Также вы должны включить расчет в пункт group by.

Demo

select 
    case when Reputation < 100 then 'lt_100' 
     when Reputation >= 100 and Reputation < 200 then '100_199' 
     when Reputation >= 200 and Reputation < 300 then '200_299' 
     when Reputation >= 300 and Reputation < 400 then '300_399' 
     when Reputation >= 400 and Reputation < 500 then '400_499' 
     when Reputation >= 500 and Reputation < 600 then '500_599' 
     when Reputation >= 600 and Reputation < 700 then '600_699' 
     when Reputation >= 700 and Reputation < 800 then '700_799' 
     when Reputation >= 800 and Reputation < 900 then '800_899' 
     when Reputation >= 900 and Reputation < 1000 then '900_999' 
     else 'over 1000' 
    end ReputationRange, 
    count(*) as TotalWithinRange 
FROM Users 
JOIN Posts ON Users.Id = Posts.OwnerUserId 
JOIN PostTags ON PostTags.PostId = Posts.Id 
JOIN Tags on Tags.Id = PostTags.TagId 
WHERE PostTypeId = 1 and Posts.CreationDate > '9/1/2010' 
Group by 
case when Reputation < 100 then 'lt_100' 
     when Reputation >= 100 and Reputation < 200 then '100_199' 
     when Reputation >= 200 and Reputation < 300 then '200_299' 
     when Reputation >= 300 and Reputation < 400 then '300_399' 
     when Reputation >= 400 and Reputation < 500 then '400_499' 
     when Reputation >= 500 and Reputation < 600 then '500_599' 
     when Reputation >= 600 and Reputation < 700 then '600_699' 
     when Reputation >= 700 and Reputation < 800 then '700_799' 
     when Reputation >= 800 and Reputation < 900 then '800_899' 
     when Reputation >= 900 and Reputation < 1000 then '900_999' 
     else 'over 1000' 
end 
+0

Кроме того, удалите части 'Репутация> = xxx и', это 'CASE' :) – dnoeth

+0

Спасибо @vkp! Кроме того, я не понимал различие SQL между одиночными и двойными кавычками. –

+0

также обратите внимание, что при использовании «двойных кавычек» вокруг имен столбцов он учитывает регистр. –

1

Unfortunatley вы не можете псевдоним группы с помощью «1», как вы можете в порядке по. - Для того, чтобы избежать повторения случая заявление, в вашей группе, вы можете использовать в «с» в операторе SQL:

with data as (
select 
    case when Reputation < 100 then 'lt_100' 
     when Reputation >= 100 and Reputation < 200 then '100_199' 
     when Reputation >= 200 and Reputation < 300 then '200_299' 
     when Reputation >= 300 and Reputation < 400 then '300_399' 
     when Reputation >= 400 and Reputation < 500 then '400_499' 
     when Reputation >= 500 and Reputation < 600 then '500_599' 
     when Reputation >= 600 and Reputation < 700 then '600_699' 
     when Reputation >= 700 and Reputation < 800 then '700_799' 
     when Reputation >= 800 and Reputation < 900 then '800_899' 
     when Reputation >= 900 and Reputation < 1000 then '900_999' 
     else 'over 1000' 
     end as ReputationRange FROM Users 
JOIN Posts ON Users.Id = Posts.OwnerUserId 
JOIN PostTags ON PostTags.PostId = Posts.Id 
JOIN Tags on Tags.Id = PostTags.TagId 
WHERE PostTypeId = 1 and Posts.CreationDate > '9/1/2010') 
select ReputationRange, count(*) as TotalWithinRange 
from data 
Group by ReputationRange 

Working Demo/Example