SQL:按分段指标堆叠列

时间:2015-06-25 16:25:49

标签: mysql sql buckets

所以我使用带有图表工具的SQL,我无法得到我的度量标准,以反映y轴(作为堆积列)中的总和与x轴中的度量的比例。

示例:度量标准(x轴值)应为“作业计数”,“访问”,“应用程序”和“支出”。每个人都应该有一个基于以下访问量指标桶的堆积列:

1-2次访问,3-4次访问,5-7次访问,8-10次访问,11-15次访问,16-25次访问,以及25次访问。

我的查询如下:

select
  alias.grp as Bucket
  , count(alias.jobid) as Number_Of_Jobs_by_Bucket
  , sum(alias.total_visits) as Visits_by_Bucket
  , sum(alias.spend) as Spend_by_Bucket
  , sum(alias.Total_applications) as Apps_by_Bucket
from
  (
    select
      analytics.jobid
      , case
        when sum(analytics.visits) >= 1
        and sum(analytics.visits) <= 2
          then 'A: 1-2 Visits'
        when sum(analytics.visits) > 2
        and sum(analytics.visits) <= 4
          then 'B: 3-4 Visits'
        when sum(analytics.visits) > 4
        and sum(analytics.visits) <= 7
          then 'C: 5-7 Visits'
        when sum(analytics.visits) > 7
        and sum(analytics.visits) <= 10
          then 'D: 8-10 Visits'
        when sum(analytics.visits) > 10
        and sum(analytics.visits) <= 15
          then 'E: 11-15 Visits'
        when sum(analytics.visits) > 15
        and sum(analytics.visits) <= 25
          then 'F: 16-25 Visits'
        when sum(analytics.visits) > 25
          then 'G: 25+ Visits'
      end as grp
      , sum(analytics.visits) as Total_Visits
      , sum(analytics.total_spend100)/100 as Spend
      , sum(analytics.applications) as Total_Applications
    from
      analytics
    where
      visits > 0
    group by
      analytics.jobid
  )
  as alias
where
  alias.total_visits > 0
group by
  alias.grp
order by
  alias.grp

因此,我尝试使用子查询来定义存储桶,但不能将每个度量标准的百分比按桶划分为Y轴(由于它是文本值)。理想情况下,我会显示四列(按指标),每一列显示例如第一个桶的10%,第二个桶的25%,第三个桶的4%等。

有关改进查询以支持此问题的想法吗?

谢谢!

0 个答案:

没有答案