【问题标题】:SQL sum up sales volume for different customer locationsSQL汇总不同客户位置的销售量
【发布时间】:2022-11-17 17:45:10
【问题描述】:

我正在使用 Oracle SQL-developer,我得到了以下输出表,它显示了我们客户的月销售额。客户有几个位置。

month year customer_name sales_volume
01 2022 Farming company Berlin 150
01 2022 Farming company London 200
01 2022 Farming company Amsterdam 350
01 2022 XY Company Berlin 200
01 2022 customer 5 7
01 2022 customer 7 7
01 2022 X_Person 2
02 2022 XY Company London 100
02 2022 Hello Company Berlin 150
02 2022 Hello Company Amsterdam 150
02 2022 customer 1 20
02 2022 customer 2 10
02 2022 customer 3 5
02 2022 Y-Person 1

现在我想获得每个 customer_name 的月/年的 sales_volume。我想为 Farming 公司、XY 公司和 Hello 公司的所有不同地点添加每月/每年的 sales_volume。其余(客户 1-7、X 人、Y 人)应汇总在名为“其他”的自己的行中

新的输出表如下:

month year customer_name sum_Sales_volume
01 2022 Farming Company 700
01 2022 XY Company 300
01 2022 Other 16
02 2022 XY Company 100
02 2022 Hello Company 300
02 2022 Other 36

到目前为止,我试图将 customer_name 与 LIKE 函数相加,但我不明白在这种情况下“when then”是如何工作的。

我的代码:

Select 
month, 
year, 
sum(sales_volume)
CASE
    WHEN customer_name    LIKE  '%Farming%' Then 'Farming Company' 
    WHEN customer_name    LIKE  '%XY%' Then 'XY Company' 
    WHEN customer_name   LIKE  '%Hello%' Then 'Hello Company' 
    ELSE THEN 'Standard'
END AS "sum_Sales_volume"

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    我不明白“何时”在这种情况下是如何工作的。

    LIKE 比较中,% 是一个通配符,可以匹配零个或多个任意字符。

    因此,对于 WHEN customer_name LIKE '%Farming%',您正在测试 customer_name 值是否具有:

    • 字符串的开始;然后
    • 零个或多个任意字符;然后
    • 子串Farming;然后
    • 零个或多个任意字符;和
    • 最后,字符串结尾。

    如果匹配,则返回 CASE 表达式的 THEN 子句的值;否则测试下一个 WHEN 子句,依此类推,直到没有匹配 WHEN 子句,然后返回 ELSE 子句的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2022-11-30
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多