【发布时间】:2021-10-20 00:47:50
【问题描述】:
我有一个问题。我的桌子是:
| Keyword | Volume |
|---|---|
| peanut butter cookies | 246000 |
| peanut butter falcon | 201000 |
| peanut butter | 110000 |
| peanut cookie recipe | 90500 |
| the peanut butter falcon | 90500 |
| butter blossoms | 74000 |
| peanut butter whiskey | 74000 |
我的 SQL 查询:
Select
Case when Keyword like '%peanut%' then 'peanut'
Case when Keyword like '%butter%' then 'butter'
Case when Keyword like '%falcon%' then 'falcon'
ELSE 'other' END as cluster,
count(Keyword) as count_Keyword
from tablename
group by cluster
我的输出
| Cluster | count_Keyword |
|---|---|
| peanut | 6 |
| butter | 1 |
我正在寻找不互斥的解决方案并返回以下内容:
| Cluster | count_Keyword |
|---|---|
| peanut | 6 |
| butter | 6 |
| Falcon | 2 |
【问题讨论】:
-
CASE表达式; T-SQL 不支持Case(Switch) 语句。
标签: sql sql-server tsql case