I have a sql query which produces the following table, But I want to be able to obtain the total for the marked columns which are grouped by category:

我有一个sql查询,它生成下面的表,但是我希望能够获得按类别分组的标记列的总数:

here is the code:

这是代码:

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

2 个解决方案

#1


4

You can do this using the sum() windows function. You can actually nest it with regular aggregation functions. So:

可以使用sum() windows函数来实现这一点。实际上,您可以用常规的聚合函数来嵌套它。所以:

sum(count(it.ID)) over () as TotalCnt,
sum(sum(mc.ots)) over () as TotalImps

This assumes that you are using SQL Server 2005 or greater.

这假定您正在使用SQL Server 2005或更高版本。

That does it for the "whole column". For a subgroup, such as those defined by a category, the same idea holds (as pointed out by Alexander):

这对于“整列”是成立的。对于一个子组,例如由一个类别定义的子组,同样的想法是成立的(如Alexander指出的):

sum(count(it.ID)) over (partition by category) as CategoryCnt, sum(sum(mc.ots)) over (partition by category) as CategoryImps

sum(count(it.ID))除以(partition by category)作为CategoryCnt, sum(sum(sum(sum(sum(sum)))除以(partition by category)作为category

更多相关文章

  1. mysql自定义排序规则函数——field()
  2. oracle基础知识总结 part 3 : 三范式,PLSQL,存储过程,函数,触发器
  3. sql 内置函数 获取表的 列名
  4. Java se之静态代码块、代码块、构造函数执行顺序问题
  5. 关于JAVA回调函数的使用
  6. 如何在函数中将两个参数从1个类传递给另一个?
  7. 回调函数在Android监听机制中的体现
  8. javaScript函数中执行C#代码中的函数
  9. JavaScript unshift()函数移入数据到数组第一位

随机推荐

  1. 你真的了解概率吗?
  2. SQL今日一题(17):涨幅
  3. 七夕用python给男朋友写的小程序,感动哭了
  4. SQL今日一题(18):3表连接
  5. 离散型随机变量的概率分布
  6. 如何用算法改变生活 | 算法之美
  7. SQL今日一题(15):子查询
  8. QQ音乐评论爬取
  9. 英雄联盟皮肤大拼图
  10. 数据分析都会用到哪些工具?