select CustomerKey, FirstName, LastName
from DimCustomer
where CustomerKey in
(select distinct CustomerKey
from FactInternetSales
where year(OrderDate) = 2005 or year(OrderDate) = 2006
or year(OrderDate) = 2007 or year(OrderDate) = 2008);
我应该在哪里添加“超过30个订单”部分?
答案 0 :(得分:1)
通过使用联接和聚合过滤器尝试以下操作
select DimCustomer.CustomerKey,
DimCustomer.FirstName, DimCustomer.LastName,year(FactInternetSales.OrderDate),count(*)
from DimCustomer join FactInternetSales
on DimCustomer.CustomerKey=FactInternetSales.CustomerKey
group by CustomerKey, FirstName, LastName,year(FactInternetSales.OrderDate)
having count(*)>=30
答案 1 :(得分:0)
您的方法可行,但是您需要group by
,而不是select distinct
:
select c.CustomerKey, c.FirstName, c.LastName
from DimCustomer c
where c.CustomerKey in (select fis.CustomerKey
from FactInternetSales fis
where fis.OrderDate >= '2005-01-01' and
fis.OrderDate < '2009-01-01'
group by year(fis.OrderDate)
having count(*) >= 30
);
如果要将结果限制为特定时间段,请在子查询中使用where
子句。