Sql 按分组的不同计数
我已经搜索过了,但没有找到我问题的答案。我的问题是,如果我使用下面的查询,我会得到正确的计数,即90:Sql 按分组的不同计数,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我已经搜索过了,但没有找到我问题的答案。我的问题是,如果我使用下面的查询,我会得到正确的计数,即90: select count(distinct account_id) from FactCustomerAccount f join DimDate d on f.date_id = d.datekey -- 90 但当我按日历年分组如下时,我遗漏了12个计数。查询和输出如下: select CalendarYear,count(distinct account_id) as accoun
select count(distinct account_id)
from FactCustomerAccount f
join DimDate d on f.date_id = d.datekey
-- 90
但当我按日历年分组如下时,我遗漏了12个计数。查询和输出如下:
select CalendarYear,count(distinct account_id) as accountCount
from FactCustomerAccount f
join DimDate d on f.date_id = d.datekey
group by CalendarYear
output:
CalendarYear accountCount
2005 10
2006 26
2007 49
2008 63
2009 65
2010 78
我不知道为什么我错过了12项。要调试,如果FactCustomerAccount中缺少date_id,但未找到缺少的键,则运行以下查询:
select distinct f.date_id from FactCustomerAccount f
where f.date_id not in
(select DateKey from dimdate d)
我正在使用SQLServer2008R2。
有谁能告诉我遗漏12项的原因吗?
提前谢谢
编辑一个:
我不太理解在2个回复中给出的问题的原因/答案,因此我想在下面使用AdventureWorksDW2008R2添加2个查询,其中不缺少任何计数:
select count (distinct EmployeeKey)
from FactSalesQuota f
join dimdate d on f.DateKey = d.DateKey
-- out: 17
select d.CalendarYear, count (distinct EmployeeKey) as Employecount
from FactSalesQuota f
join dimdate d on f.DateKey = d.DateKey
group by d.CalendarYear
-- out:
-- CalendarYear Employecount
-- 2005 10
-- 2006 14
-- 2007 17
-- 2008 17
因此,请纠正我遗漏的内容。您的查询非常不同: 第一:
select count(distinct account_id)
from FactCustomerAccount f
join DimDate d on f.date_id = d.datekey
返回所有年份中不同帐户的计数,因此如果您在两年内有帐户id,则返回1个计数
第二点:
按日历年分组,因此,如果您在两个不同的年份中有一个帐户id,则此信息将分为两行
select CalendarYear,count(distinct account_id) as accountCount
from FactCustomerAccount f
join DimDate d on f.date_id = d.datekey
group by CalendarYear
编辑
我试图更好地解释:
我想这是订单对的数据集:年份,账户id
`2008 10`
`2009 10`
`2010 10`
`2010 12`
如果运行两个以上的查询,则有:
`2`
及
因为存在两个不同的帐户id 10和12,并且只有在去年2010年,帐户id 10和12才写入了它们的行
但如果您有此数据集:
`2008 10`
`2009 10`
`2009 12`
`2010 12`
您将有:
第一个查询结果:
二,
第二个查询结果:
2008 1
2009 2
2010年1月您的查询非常不同: 第一:
select count(distinct account_id)
from FactCustomerAccount f
join DimDate d on f.date_id = d.datekey
返回所有年份中不同帐户的计数,因此如果您在两年内有帐户id,则返回1个计数
第二点:
按日历年分组,因此,如果您在两个不同的年份中有一个帐户id,则此信息将分为两行
select CalendarYear,count(distinct account_id) as accountCount
from FactCustomerAccount f
join DimDate d on f.date_id = d.datekey
group by CalendarYear
编辑
我试图更好地解释:
我想这是订单对的数据集:年份,账户id
`2008 10`
`2009 10`
`2010 10`
`2010 12`
如果运行两个以上的查询,则有:
`2`
及
因为存在两个不同的帐户id 10和12,并且只有在去年2010年,帐户id 10和12才写入了它们的行
但如果您有此数据集:
`2008 10`
`2009 10`
`2009 12`
`2010 12`
您将有:
第一个查询结果:
二,
第二个查询结果:
2008 1
2009 2
2010年1号你不会错过12号。可能是一些账户在最后几年没有活动 你不会错过12个。可能是一些账户在最后几年没有活动 为了分析这一点,我会说,检查行数。检查日历列。日历年中是否有空的行。或者尝试排名,我不确定
select *,
ROW_NUMBER()over(partition by CalendarYear,account_id order by CalendarYear)
from FactSalesQuota f
join dimdate d on f.DateKey = d.DateKey
为了分析这一点,我会说,检查行数。检查日历列。日历年中是否有空的行。或者尝试排名,我不确定
select *,
ROW_NUMBER()over(partition by CalendarYear,account_id order by CalendarYear)
from FactSalesQuota f
join dimdate d on f.DateKey = d.DateKey
这只是一个例子。2008年DB distinct EmployeeKey为17,与所有distinct EmployeeKey的数量相同。莎玛:在我的回答中显示整合这只是一个例子。2008年DB distinct EmployeeKey为17,与所有distinct EmployeeKey的数量相同。沙玛:在我的答案上显示积分