我试图做到以下几点:SQL包括数0如果不是有
在这个国家
选择所有产品的这家制造商在这个国家的每一件产品计算点击次数的时间指定期限。
如果计数为0,显示为0。
所以我satrted这样的:
-- 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
,并用p选择RODUCT并做了左连接:
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点击数,所以我认为它一定是因为我有widgetclick表在where子句中..?
所以我想我的表看起来像
Product Name Widget Name Click Count
Product1 Widget1 73
Product2 Widget1 0
Product3 Widget1 4
Product4 Widget1 2
任何帮助,非常感谢!
我从来没有考虑移动的条件进入加入谢谢你强调这一点,漂亮说明! – anna