【问题标题】:Group the sum of sales of different customer age groups (SQL)对不同客户年龄段的销售额总和进行分组(SQL)
【发布时间】:2022-11-30 08:54:23
【问题描述】:

希望有人可以帮助我,因为我继续学习 sql。

我有一个数据集,其中包含不同客户的销售额以及他们各自的年龄。

我的目标是以 10 岁(40 岁、50 岁、60 岁、70 岁等)为间隔将客户年龄分组在一起,然后显示每个年龄组的销售额总和。

Here's what I was able to do so far

我正在努力将年龄组和销售分组在一起。理想情况下,上面的结果将显示 40-49 岁的年龄范围以及这些销售额的总和。

使用 Microsoft SQL Server 2019

在此先感谢您的帮助,非常感谢。

【问题讨论】:

    标签: sql sum range


    【解决方案1】:

    像这样的 -

    SELECT 
        case when cast("Customer Age" as int) between 40 and 49 then '40s' 
            when cast("Customer Age" as int) between 50 and 59 then '50s'
            when cast("Customer Age" as int) between 60 and 69 then '60s'
                end as age_group
        ,sum(Sales) AS Revenue
    FROM dbo.customerdata
    GROUP BY 
        case when cast("Customer Age" as int) between 40 and 49 then '40s' 
            when cast("Customer Age" as int) between 50 and 59 then '50s'
            when cast("Customer Age" as int) between 60 and 69 then '60s'
                end
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 2019-07-24
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多