繁体   English   中英

具有 Null 值和 Integer 的案例语句

[英]Case statement with Null value and Integer

我有以下工作正常的测试用例:

WITH t1 AS (
  SELECT 1 as id
  UNION ALL
  SELECT NULL
  UNION ALL
  SELECT 2
)

Select 
  *,
  case when id is null then "null" when id = 1 then "test" end
from 
  t1

在我的真实查询中,我有一个 date_diff function:

DATE_DIFF(second_date,first_date, DAY) as time_lag,

并在尝试应用以下 case 语句时:

case when time_lag is null then "null" when time_lag = "1" then "test" end

我有这个错误:

No matching signature for operator = for argument types: STRUCT<client_ID STRING, Total INT64, first_date DATE, ...>, STRING. Supported signatures: ANY = ANY at [79:49]

据我了解,DATE_DIFF 语句返回INT64和一些 null 值,因为并非所有行都包含日期。 我不明白我的测试用例与我的实际用例有何不同。 我的真实用例有什么问题......

你应该试试

case when time_lag is null then "null" when time_lag = 1 then "test" end    

正如你所说 - time_lag 是 INT64 所以问题出在/是... when time_lag = "1" then...

根据我的评论,您将 time_lag 视为数据库表中的一列,我猜它不是。 它是从 2 列计算得出的值。 因此,您有 2 个选择 - 要么使用执行 DATE_DIFF 操作的子查询,然后将其别名为 time_lag。 或者在case表达式中直接使用计算。

 CASE WHEN DATE_DIFF(second_date,first_date, DAY) is null THEN null WHEN DATE_DIFF(second_date,first_date, DAY) = 1 then 'Test' end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM