我的问题是,我们当前的数据源如何具有最低的粒度级别。以目标或后端数据库方面的形式显示数据是一项任务,但缺乏言语。仍然;我真的不想创建sql形式的自定义数据源来完成这项工作,但想知道是否可以使用原始表/视图作为源来实现它。 还找到附上我的sql代码。我在Tableau上尝试过的是: 1)if语句为每个Customer_number创建一个存储桶。 2)试图对上面的1号进行固定公式计算会导致错误,因为它是一个计算字段。字段内的公式是这样的
IF count(Customer_number) = 1 then '1-Visit'
else if count(Customer_number) = 2 then '2-Visit' ...
else '13+Visits' end.
3)试图通过STR()函数将2号中的每个强制转换为字符串。也没有运气。
所以我如何在Tableau中实现此功能而无需执行自定义黑客工作。
select Buckets
,No_Of_Purchases
,Total_Spend
,Avg_Annual_Total_Spend = Total_Spend/No_Of_Purchases
from (
select Buckets
,No_Of_Purchases = count(No_Of_Purchases)
,Total_Spend = sum(Total_Spend)
from (
select *
,Buckets = case when No_Of_Purchases = 1 then '1 Visit'
when No_Of_Purchases = 2 then '2 Visit'
when No_Of_Purchases = 3 then '3 Visit'
when No_Of_Purchases = 4 then '4 Visit'
when No_Of_Purchases = 5 then '5 Visit'
when No_Of_Purchases = 6 then '6 Visit'
when No_Of_Purchases = 7 then '7 Visit'
when No_Of_Purchases = 8 then '8 Visit'
when No_Of_Purchases = 9 then '9 Visit'
when No_Of_Purchases = 10 then '10 Visit'
when No_Of_Purchases = 11 then '11 Visit'
when No_Of_Purchases = 12 then '12 Visit'
Else '13+ Visit' end
from (
select [cust_num]
,[cust_acct_card_nbr]
,[vip_flag]
,
Total_Spend = Sum([base_total])
,Distinct_Cust_Acct_Card_nbr = count( distinct [cust_acct_card_nbr])
,No_Of_Purchases = count(Cust_Acct_Card_nbr )
,Total_VipCount = sum(case when [vip_flag] = 1 then 1 else 0 end)
--,Total_Non_VipCount = case when [vip_flag] = 0 then 1 else 0 end
from stg.header a (nolock)
where [vip_flag] = 1
and year(business_date) = 2018
group by [cust_num]
,[cust_acct_card_nbr]
,[vip_flag]
) a
) b
group by Buckets
) c