【发布时间】:2020-02-24 11:56:23
【问题描述】:
我正在尝试使用数据透视表按位置获取客户访问计数的结果,但 count(distinct customer_id) 和 count(customer_id) 返回相同的结果。我该怎么办?我应该只为那部分编写另一个子查询吗?
select Locationname as 'Location',[2020],[2019],[2018],[2017],[2016],[2015],[2014],[2013],[2012],[2011],[2010]
from (select year(date_when) [date_when2],Locationname ,count(distinct customer_id) [cst_id] from record
group by date_when,Locationname) as aws
PIVOT(
count([cst_id])
FOR [date_when2] IN (
[2020],
[2019],
[2018],
[2017],
[2016],
[2015],
[2014],
[2013],
[2012],
[2011],
[2010]
)
) AS pivot_table
按要求编辑: 这是查询的当前结果,但是我想查看唯一客户数,并且 distinct 没有区别。
【问题讨论】:
-
请提供样本数据和期望的结果。
标签: sql sql-server select pivot