【问题标题】:Alternative for case when statement that is not mutually exclsuive不互斥的 case when 语句的替代方案
【发布时间】: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


【解决方案1】:

为什么不只是 COUNTLIKE

SELECT COUNT(CASE WHEN Keyword LIKE '%peanut%' THEN 1 END) AS Peanut,
       COUNT(CASE WHEN Keyword LIKE '%butter%' THEN 1 END) AS Butter,
       COUNT(CASE WHEN Keyword LIKE '%Falcon%' THEN 1 END) AS Falcon
FROM dbo.YourTable;

【讨论】:

  • 谢谢。我的实际查询还包括其他一些选择,例如 sum(Volume) count(Keyword) as count_Keyword, sum(Volume) as Volume from tablename group by cluster
  • 您可以添加其他聚合,如果您愿意,@wouterbuijze,这回答了您提出的问题。
  • 好的,再次感谢,对不起,我是个初学者。这会是什么样子?以下不适用于我.. SELECT COUNT(CASE WHEN Keyword LIKE '%peanut%' THEN 1 END) AS Peanut, COUNT(CASE WHEN Keyword LIKE '%butter%' THEN 1 END) AS Butter, COUNT(CASE WHEN Keyword LIKE '%Falcon%' THEN 1 END) AS Falcon, SUM(CASE WHEN Keyword LIKE '%peanut%' THEN Volume END) AS Peanut_Volume, SUM(CASE WHEN Keyword LIKE '%butter%' THEN Volume END) AS Butter_ Volume, SUM(CASE WHEN Keyword LIKE '%Falcon%' THEN Volume END) AS Falcon_ Volume FROM dbo.YourTable;
  • “不工作”是什么意思@wouterbuijze?
  • 仅仅是您的印刷错误,例如AS Butter_ Volume@wouterbuijze 吗? (注意下划线后面的空格。)
【解决方案2】:

您可以将关键字模式存储在派生表中并使用join

Select c.cluster, count(t.keyword)
from tablename t
     (select 'peanut' as cluster union all
      select 'butter' union all
      select 'falcon'
     ) c left join
     tablename t
     on t.keyword like concat('%', c.cluster, '%')
group by c.cluster;

【讨论】:

    【解决方案3】:

    这可以解决您的问题吗?

    http://sqlfiddle.com/#!9/859eae

    create table dummy(
      Keyword varchar(255),
      Volumne int
    );
    
    insert into dummy
    values
      ('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);
    
    select
      c.cluster, count(t.Keyword) as cnt
    from
      (
          select 'peanut' as cluster union all
          select 'butter'            union all
          select 'falcon'
      ) c
        left join dummy t
        on t.Keyword like concat('%', c.cluster, '%')
    group by c.cluster;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多