如何将非相关列添加到组?

时间:2013-06-26 07:45:26

标签: sql sql-server tsql

我想通过查询将以下非关联列添加到组中但我不希望结果按以下列分组。

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

我该怎么做?

当前导致'Column'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 个答案:

答案 0 :(得分: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