首页 > 解决方案 > 在 SQL 中使用带大小写的 IN 运算符?

问题描述

我正在尝试运行与IN运算符一起使用的 case when 语句 - 这样,当URL向 IN 子句提供值(或值)时,当它们匹配时,它会在给值 1 或 Null 时执行这种情况提供的 URL 或一组 URL。

我比较纠结于如何在IN操作时实际使用操作符。我希望能够在具有 url 的地方提供多个值 - 而不是一个。这可能吗?

我的代码如下:

create temp table ag_table as 
   with sq AS(select id, business, 
   cat1, cat2, channel, call_reason, 
   COUNT(case cat1 when url then 1 else null end) 
   over (partition by subject 
order by 
time range between 604800 preceding
and current row) as count 
from table_name
where table_name.start >= start and table.end_time <= end
)

注意我正在使用 SQLite

标签: sqlsqlite

解决方案


这是 CASE 表达式的语法:

COUNT(case when category1 IN (url1, url2, ...) then 1 end)

不需要else null(null 是没有 else 时的默认值)。
同样可以通过以下方式实现:

SUM(category1 IN (url1, url2, ...))

推荐阅读