-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
4

1 回答 1

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
于 2013-06-26T08:23:18.977 回答