【发布时间】: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"
【问题讨论】: