-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 ステートメントが 1 つの値のみを返すことが保証されている場合は、その値を max() 関数でラップしてみることができます。max() は文字列でも機能します。

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 に答える