0

我想创建一个图表来代表一些商店的表现。x 轴将代表商店的总收入超过所有商店的平均收入。

y 轴将表示特定商店的客户访问该商店的平均频率超过所有客户访问所有商店的平均频率。

这是我尝试过的:

select 
distinct [Order].shop_id 
, SUM(total) /((select AVG(o.shopTotal) 
                            from 
                            (
                            select 
                                    distinct shop_id , SUM(total) as shopTotal 
                                    from [Order] 
                                    group by shop_id
                            )o)*0.1) as revPerAvgRev

,COUNT(distinct orderno) /((select AVG(orders) 
                            from 
                            (
                            select 
                                    distinct shop_id , room, COUNT(distinct orderno) as orders 
                                    from [Order] 
                                    group by shop_id , room
                            )o)*0.1) as freqPerAvgFreq
from [Order] 
group by [order].shop_id
order by revPerAvgRev desc

select distinct shop_id ,room , count(distinct orderno)
from [Order] 
group by shop_id , room

我相信顾客光顾商店的平均频率是可以的。但我无法正确计算每位顾客光顾商店的平均频率

注意:每位顾客只光顾一家商店。

4

1 回答 1

1

准确地遵循你想要的有点困难。您要求频率,但代码示例显示收入。您可以通过使用窗口函数计算总和得到收入的比例:

select shop_id, shopTotal,
       cast(shopTotal as float)/ sum(shopTotal) over () as Revenue,ProportionOfTotal,
       numvisits,
       cast(numvisits as float) / sum(numvisits) over () as VisitsProportionOfTotal
from (select shop_id , SUM(total) as shopTotal, count(*) as numvisits
      from [Order] 
      group by shop_id
     ) s
于 2013-05-27T15:03:32.897 回答