-6

我有查询返回有关整体销售的结果,但现在我想通过添加DATE间隔来过滤它们。

编辑:fs.DateKeyDateTime数据类型

我原来的查询:

SELECT s.storekey, 
       e.employeekey, 
       e.parentemployeekey, 
       pc.productcategorykey, 
       pc.productcategoryname, 
       Sum(fs.salesamount)AS SalesAmount 
FROM   dimstore s 
       INNER JOIN factsales fs 
               ON fs.storekey = s.storekey 
       RIGHT JOIN dimemployee e 
               ON e.employeekey = s.storemanager 
       INNER JOIN dimproduct p 
               ON p.productkey = fs.productkey 
       INNER JOIN dimproductsubcategory psc 
               ON psc.productsubcategorykey = p.productsubcategorykey 
       INNER JOIN dimproductcategory pc 
               ON pc.productcategorykey = psc.productcategorykey 
GROUP  BY s.storekey, 
          e.employeekey, 
          e.parentemployeekey, 
          pc.productcategoryname, 
          pc.productcategorykey 
ORDER  BY employeekey 

我正在考虑添加WHERE fs.DateKey BETWEEN '2007-01-20' AND '2007-01-25',但由于某种原因结果不正确。我不明白为什么。有什么提示或建议可以达到预期的效果吗?

谢谢!

4

1 回答 1

11

发布作为答案,因为我有很多评论:

RIGHT JOIN肯定是错的。这意味着某些商店不需要存在,但当然,您的销售事实与带有INNER JOIN. 它实际上已经变成了INNER JOIN无论如何,因此不太可能成为您问题的根源。

The fact that you used a RIGHT JOIN tells me that you think there could be a case where a foreign key is optional in some of the facts or dimensions. I'd like to know why this is.

In any case, in a straightforward star model, you usually only see INNER JOINs and sometimes LEFT JOINs.

Because the fact table is at the center of the star, I would usually list that first in the SELECT, especially if there are LEFT JOINs out from the fact table.

Since it is a datawarehouse, and you probably have a lot of data, I'm not sure how much you can post so we can understand your expectations versus the results you are getting, but be aware that using BETWEEN includes both endpoints and that DATETIME datatype can contain a time portion. Because of this, I almost always prefer to use the notation dt >= start_date AND dt < end_date. It's typically no performance difference, since BETWEEN is basically syntactic sugar for dt >= start_date AND dt <= end_date.

于 2013-03-06T18:53:49.137 回答