2013-06-26 3 views
-1

다음과 같은 비 relavant 열을 쿼리에 의해 그룹에 추가하고 싶지만 그 결과가 다음 열로 그룹화되기를 원하지 않습니다.그룹별로 비 관련 열을 추가하는 방법은 무엇입니까?

case when ito.Rating<=25    then 'e - Very Unfavourable' 
when ito.rating>=30 and Rating<=45 then 'd - Unfavourable' 
when ito.rating = 50    then 'c - Neutral' 
when ito.rating>=55 and Rating<=70 then 'b - Favourable' 
when ito.Rating>=75     then 'a - Very Favourable' 
end as ComplexFav 

어떻게해야합니까? 현재 '열'ItemOrganisations.Rating 결과

전체 스크립트는 '이 집계 함수 나 GROUP BY 절 중 하나에 포함되지 않기 때문에 선택 목록에서 유효하지 않습니다.': 그냥

select o.Name, 

case when ito.Rating<=25    then 'e - Very Unfavourable' 
    when ito.rating>=30 and Rating<=45 then 'd - Unfavourable' 
    when ito.rating = 50    then 'c - Neutral' 
    when ito.rating>=55 and Rating<=70 then 'b - Favourable' 
    when ito.Rating>=75     then 'a - Very Favourable' 
    end as ComplexFav, 


COUNT(i.ID) as 'total count', 
sum(mc.Circulation/ 1000) as Imps, 

sum(case when ito.Rating >50 then 1 else 0 end) as 'fav count', 
sum(case when ito.rating <50 then 1 else 0 end) as 'unfav count', 
sum(case when ito.Rating =50 then 1 else 0 end) as 'neu count', 

avg(CONVERT(decimal(6,2),ito.Rating)) as 'Av Rating P', 

(sum(case when ito.Rating >50 then 1.0 else 0.0 end)/count(i.ID) * 100) as 'fav P', 
(sum(case when ito.rating < 50 then 1.0 else 0.0 end)/count(i.ID) * 100) as 'unfav P', 
(sum(case when ito.Rating =50 then 1.0 else 0.0 end)/count(i.ID) * 100) as 'neu P', 

sum(case when ito.Rating >50 then mc.Circulation/1000 else 0 end) as 'fav Imps', 
sum(case when ito.rating <50 then mc.Circulation/1000 else 0 end) as 'unfav Imps', 
sum(case when ito.Rating =50 then mc.Circulation/1000 else 0 end) as 'neu Imps', 

CASE WHEN ISNULL(sum(mc.circulation/1000),0) = 0 THEN 0 ELSE (sum(case when ito.Rating > 50 then mc.Circulation /1000 else 0.0 end)/(sum(mc.Circulation/1000)) * 100) END as 'fav Imps P', 
CASE WHEN ISNULL(sum(mc.circulation/1000),0) = 0 THEN 0 ELSE (sum(case when ito.rating < 50 then mc.Circulation /1000 else 0.0 end)/(sum(mc.Circulation/1000)) * 100) END as 'unfav Imps P', 
CASE WHEN ISNULL(sum(mc.circulation/1000),0) = 0 THEN 0 ELSE (sum(case when ito.Rating = 50 then mc.Circulation /1000 else 0.0 end)/(sum(mc.Circulation/1000)) * 100) END as 'neu Imps P' 

from 
Profiles P WITH(NOLOCK) 
INNER JOIN ProfileResults PR WITH(NOLOCK) ON P.ID = PR.ProfileID 
INNER JOIN Items i WITH(NOLOCK) ON PR.ItemID = I.ID 
inner join ItemOrganisations ito WITH(NOLOCK) on i.ID= ito.ItemID 
inner join organisations o WITH(NOLOCK) on ito.OrganisationID = o.ID 
inner join Batches b WITH(NOLOCK) on b.ID=i.BatchID 
inner join Lookup_ItemStatus lis WITH(NOLOCK) on lis.ID = i.StatusID 
inner join Lookup_BatchStatus lbs WITH(NOLOCK) on lbs.ID = b.StatusID 
inner join Lookup_BatchTypes bt WITH(NOLOCK) on bt.id = b.Typeid 
inner join Lookup_MediaChannels mc WITH(NOLOCK) on mc.ID = i.MediaChannelID 

where p.ID = 191377 
and b.StatusID IN (6,7) 
and i.StatusID = 2 
and i.IsRelevant = 1 
and ito.Rating is not null 

group by o.name 
+1

좋아요. 즉,'ito.Rating' 열에는 각 그룹에 * 여러 개의 값이있을 수 있습니다. * 여러 * 값 중에서 **이 새 열 내에서 표현식을 평가하는 데 사용해야하는 값은 무엇입니까? –

답변

1

추측, 그것을 직접 시도하지 않았다.

CASE 문이 하나의 값만 반환하도록 보장되는 경우 max() 함수로 래핑하려고 할 수 있습니다. max()는 AFAIK에서도 작동합니다.

max(case when ito.Rating<=25    then 'e - Very Unfavourable' 
when ito.rating>=30 and Rating<=45 then 'd - Unfavourable' 
when ito.rating = 50    then 'c - Neutral' 
when ito.rating>=55 and Rating<=70 then 'b - Favourable' 
when ito.Rating>=75     then 'a - Very Favourable' 
end) as ComplexFav 
관련 문제