Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL GROUP BY用于计算行的数量,而不是数量的总和_Sql_Group By - Fatal编程技术网

SQL GROUP BY用于计算行的数量,而不是数量的总和

SQL GROUP BY用于计算行的数量,而不是数量的总和,sql,group-by,Sql,Group By,如果一个国家的一个小部件的类别中有多个小部件,我尝试通过分组获得计数1 为了更好地解释这一点,以下是我目前获得的代码: select c.Name As Country, w.Description As WidgetName, cat.Description As Category, COUNT (wc.Id)AS Clicks from ManufacturerWidgets mw join WidgetClicks wc on mw.Id = wc.ManufacturerW

如果一个国家的一个小部件的类别中有多个小部件,我尝试通过分组获得计数1

为了更好地解释这一点,以下是我目前获得的代码:

select c.Name As Country, w.Description As WidgetName, cat.Description As Category,
  COUNT (wc.Id)AS Clicks  
  from ManufacturerWidgets mw
 join WidgetClicks wc on mw.Id = wc.ManufacturerWidgetId
 join Products p on   wc.ProductId = p.Id
 join Countries c on mw.CountryId = c.Id
 join Widgets w on w.Id = mw.WidgetId
 join Categories cat on cat.Id = p.CategoryId
 where mw.Enabled = 1 and  mw.ManufacturerId = 17 and wc.CreatedAt >= '2013-01-01 00:00:00.000' and wc.CreatedAt <= '2013-07-05 23:59:00.000'
 GRoup By wc.WidgetImpressionId, c.Name,  w.Description,  cat.Description
 HAVING wc.WidgetImpressionId > 0
 order by  c.Name, w.Description, cat.Description
我需要返回的是每个类别的inteeraction,因此:

Austria  onebysix     computing   4  - this would be 1 not 4
我需要按类别对它们进行分组,这样最终结果将是:

Austria  onebysix     computing   2
Austria  onebysix     printing    3

将wc.WidgetImpressionID从分组中删除。这是添加额外的分组级别,该级别恰好是所需结果的计数

计数更改为
计数(wc.widgetmepressionid)
分别给出2和3

HAVING应该是
HAVING COUNT(wc.widgetmepressionid)>0
。在您的代码中,它的作用类似于WHERE子句过滤器,因为它位于GROUPBY中(请参阅上面的第一点)

选择c.名称作为国家,w.描述作为WidgetName,类别描述作为类别,
将(wc.widgetmepressionid)计数为单击次数
来自制造商Widgets mw
在mw.Id=wc.ManufacturerWidgetId上加入WidgetClicks wc
在wc.ProductId=p.Id上连接产品p
在mw.CountryId=c.Id上加入国家c
在w.Id=mw.WidgetId上加入小部件w
在cat.Id=p.CategoryId上连接类别cat
其中mw.Enabled=1,mw.ManufacturerId=17,wc.CreatedAt>='2013-01-01 00:00:00.000'和wc.CreatedAt 0
按c.名称、w.说明、类别说明订购

Hi-gbn这似乎是在把点击数加在一起-所以对于计算,我得到了5次,对于打印,我得到了6次,而不是2次和3次,我是否遗漏了什么?没有样本数据,我们只能猜测。请尝试计数(DISTINCT wc.widgetmepressionid)
Austria  onebysix     computing   2
Austria  onebysix     printing    3
select c.Name As Country, w.Description As WidgetName, cat.Description As Category,
  COUNT (wc.WidgetImpressionID) AS Clicks  
  from ManufacturerWidgets mw
 join WidgetClicks wc on mw.Id = wc.ManufacturerWidgetId
 join Products p on   wc.ProductId = p.Id
 join Countries c on mw.CountryId = c.Id
 join Widgets w on w.Id = mw.WidgetId
 join Categories cat on cat.Id = p.CategoryId
 where mw.Enabled = 1 and  mw.ManufacturerId = 17 and wc.CreatedAt >= '2013-01-01 00:00:00.000' and wc.CreatedAt <= '2013-07-05 23:59:00.000'
 GRoup By c.Name,  w.Description,  cat.Description
 HAVING COUNT(wc.WidgetImpressionID) > 0
 order by  c.Name, w.Description, cat.Description