3

次のテーブルを生成する sql クエリがありますが、カテゴリ別にグループ化されたマークされた列の合計を取得できるようにしたいと考えています。 ここに画像の説明を入力

コードは次のとおりです。

select pic.Name as Category,
pis.Code as Code,
pis.Name as Issue,
count(it.ID) as  'total count',
sum(mc.ots) as 'Imps',
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',
sum(case when ito.Rating >50  then 1 else 0 end) as 'fav count',

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

CONVERT(decimal(4,2),avg(ito.Rating)) as 'Av Rating %',

count(it.id) * 100.0 / sum(count(it.ID)) OVER () AS [% of Count],
sum(mc.ots) * 100.0 / sum(sum(mc.ots)) OVER () AS [% Imps]

from 
Profiles P 
INNER JOIN ProfileResults PR ON P.ID = PR.ProfileID
INNER JOIN Items it ON PR.ItemID = It.ID
inner join Batches b on b.ID=it.BatchID
left outer join BatchActionHistory bah on b.ID=bah.batchid
inner join itemorganisations oit (nolock) on it.id=oit.itemid
inner join itemorganisations ito (nolock) on it.id=ito.itemid
inner join itemorganisationIssues ioi (nolock) on ito.id=ioi.itemorganisationid
inner join ProjectIssues pis (nolock)on ioi.IssueID = pis.ID
inner join ProjectIssueCategories pic (nolock)on pic.ID = pis.CategoryID
inner join Lookup_ItemStatus lis (nolock) on lis.ID = it.StatusID
inner join Lookup_BatchStatus lbs (nolock) on lbs.ID = b.StatusID
inner join Lookup_BatchTypes bt on bt.id = b.Typeid
inner join Lookup_MediaChannels mc on mc.id = it.MediaChannelID


where p.ID = @profileID 
and b.StatusID IN (6,7)
and bah.BatchActionID = 6
and it.StatusID = 2
and it.IsRelevant = 1


Group BY pic.Name,pis.Name,pis.Code
order by pic.name
4

2 に答える 2

2

句について読んでくださいWITH ROLLUP(またはSQL Serverの以降のリリースで利用可能なグループ化セット-使用しているものがわかりません)。列の組み合わせによって出力をグループ化しますGROUP BY(3 列のグループ化、2 列のグループ化、1 列のグループ化、および総計)。グループ化された列は、集計時に GROUPING(column_name)=1 で区別できます。お役に立てれば。

于 2013-04-26T13:36:38.830 に答える