2013-06-26 26 views
-1

我想通過查詢將以下非相關列添加到組,但我不希望結果按以下列分組。如何將不相關的列添加到組中?

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 
相關問題