where和case一起使用

ybzsozfc  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(257)

我是个初学者,很难使用 CASE 声明。这里我想用一个 CASE 如果条件(例如。 segment = '87' 或者 '30' )已满足。我试过以下方法,但没有用。
我可能在什么地方漏了逗号之类的东西吗?

WITH status AS
(
  SELECT id,
  first_day AS 'month'
  WHERE segment = '87' CASE 
    WHEN subscription_start < first_day THEN 1
    ELSE 0
  END AS is_active_87
  WHERE segment = '30' CASE 
    WHEN subscription_start < first_day THEN 1
    ELSE 0
  END AS is_active_30
);

提前谢谢!

0sgqnhkj

0sgqnhkj1#

您拥有的不是有效的sql;你需要把你的情况包括在 WHERE 进入 CASE 表情。请注意,您还缺少一个 FROM 查询中的子句:

WITH status AS
(
  SELECT id,
  first_day AS 'month',
  CASE WHEN segment = '87' AND subscription_start < first_day THEN 1
    ELSE 0
  END AS is_active_87,
  CASE WHEN segment = '30' AND subscription_start < first_day THEN 1
    ELSE 0
  END AS is_active_30
  FROM yourtable
)
kmpatx3s

kmpatx3s2#

在sqlite中,布尔表达式的计算方式为 0 为了 FALSE 或者 1 为了 TRUE ,因此不需要 CASE 表达式:

WITH status AS
(
  SELECT id,
    first_day AS month,
    (segment = '87' AND subscription_start < first_day) AS is_active_87,
    (segment = '30' AND subscription_start < first_day) AS is_active_30
  FROM tablename
)

相关问题