2013-11-25 31 views
0

我试图做到以下几点: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 

任何帮助,非常感谢!

回答

3

你快到了。考虑一下在执行GROUP BY语句之前sql所产生的中间结果集。如果WidgetClicks表中没有Product 2/Widget 1的记录,则不能使用Inner Join,因为那样您的结果集就没有任何Widget 1记录。

的LEFT JOIN是一个很好的一步,但你被淘汰的产品2 /控件1行与该位:在您所设定的

wc.CreatedAt > '2013-11-20 00:00:00.000' and wc.CreatedAt < '2013-11-25 00:00:00.000' 

该行的产品2 /控件1在的NULL wc.CreatedAt字段。 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 
+0

我从来没有考虑移动的条件进入加入谢谢你强调这一点,漂亮说明! – anna

0

尝试这样的事情
遗憾的代码没有经过测试,也许不行
它只是为eexample

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)