我试图执行以下操作:
在此国家/地区选择此制造商的所有产品
此国家/地区的每件商品均会计算指定时间段内的点击次数。
如果计数为0,则显示0。
所以我这样说:
-- yesterday data for scrollPartial/responsive (453)
select p.Description as Product, mw.Description as Widget, COUNT(wc.Id)as WidgetClicks from WidgetClicks wc
join ManufacturerWidgets mw on wc.ManufacturerWidgetId = mw.Id
join Products p on p.id = wc.ProductId
where wc.ManufacturerWidgetId = 453 and wc.CreatedAt > '2013-11-24 00:00:00.000' and wc.CreatedAt < '2013-11-25 00:00:00.000'
group by p.Description, mw.Description
order by Product
这可以获得每种产品的点击次数 - 只要点击次数不是0。
我也试过分开这些日子:
select DatePart(week, wc.Createdat) as WeekNumber, DatePart(day, wc.Createdat) as dateOfMonth, datename(dw, wc.Createdat) as WeekDayName,p.Description as Product, mw.Description as Widget, COUNT(wc.Id)as WidgetClicks from WidgetClicks wc
join ManufacturerWidgets mw on wc.ManufacturerWidgetId = mw.Id
left join Products p on p.id = wc.ProductId
where wc.ManufacturerWidgetId =453 and wc.CreatedAt > '2013-11-24 00:00:00.000' and wc.CreatedAt < '2013-11-25 00:00:00.000'
group by DatePart(week, wc.Createdat), DatePart(day, wc.Createdat), datename(dw, wc.Createdat), p.Description, mw.Description
order by WeekNumber, dateOfMonth, WeekDayName, Product
并按产品选择并进行左连接:
select p.Description as Product, COUNT(wc.ProductId) as clicks from Products p
join productcountries pc on p.Id = pc.ProductId
LEFT JOIN widgetclicks wc ON p.Id = wc.ProductId
where pc.CountryId = 231 and p.ManufacturerId = 129 and p.Discontinued = 0 and wc.ManufacturerWidgetId =453 and wc.CreatedAt > '2013-11-20 00:00:00.000' and wc.CreatedAt < '2013-11-25 00:00:00.000'
group by p.Description
如果我取消以下内容:
wc.ManufacturerWidgetId =453 and wc.CreatedAt > '2013-11-20 00:00:00.000' and wc.CreatedAt < '2013-11-25 00:00:00.000'
我可以看到0次点击次数,所以我认为必须是因为我在where子句中有widgetclick表。?
所以我希望我的表看起来像
Product Name Widget Name Click Count
Product1 Widget1 73
Product2 Widget1 0
Product3 Widget1 4
Product4 Widget1 2
非常感谢任何帮助!
答案 0 :(得分:3)
你快到了。考虑一下sql在执行GROUP BY语句之前所做的中间结果集。如果WidgetClicks表中没有Product 2 / Widget 1的记录,则不能使用Inner Join,因为那时结果集没有任何Widget 1记录。
LEFT JOIN是一个很好的一步,但是你用这个位淘汰了Product 2 / Widget 1行:
wc.CreatedAt > '2013-11-20 00:00:00.000' and wc.CreatedAt < '2013-11-25 00:00:00.000'
您的集合中的产品2 / Widget 1的行在wc.CreatedAt字段中具有NULL。 LEFT JOIN正常工作并使用Product 2 / Widget 1创建了一个中间集,但随后WHERE子句将它们过滤掉了。
解决方案是将这些条件移动到连接中。条件筛选出哪些widgetclicks记录被考虑,但由于条件不再在WHERE中,因此它不适用于包含products位的结果集。
select p.Description as Product, COUNT(wc.ProductId) as clicks
from Products p
join productcountries pc on p.Id = pc.ProductId
LEFT JOIN widgetclicks wc
ON p.Id = wc.ProductId
and wc.ManufacturerWidgetId =453
and wc.CreatedAt > '2013-11-20 00:00:00.000'
and wc.CreatedAt < '2013-11-25 00:00:00.000'
where pc.CountryId = 231 and p.ManufacturerId = 129 and p.Discontinued = 0
group by p.Description
答案 1 :(得分:0)
select p.*, wc.cnt
from Product p
left join (select wc.ProductId, count(*) as cnt
from WidgetClicks wc
where wc.ManufacturerWidgetId = 453
and wc.CreatedAt > '2013-11-24 00:00:00.000'
and wc.CreatedAt < '2013-11-25 00:00:00.000') wc
on (p.productid = wc.productid)