我有一个这样的表,
id date start end product result
10 2022-06-10 2022-02-10 2022-06-12 183 TRUE
10 2022-06-11 2022-02-10 2022-06-12 183 TRUE
10 2022-06-12 2022-02-10 2022-06-12 183 TRUE
10 2022-06-13 null null 183 FALSE
10 2022-06-14 null null 183 FALSE
......
10 2022-04-01 null null 183 FALSE
因此,当"date"列的年份和月份与"end"列的年度和月份匹配时,我希望列"result"为TRUE。
即,对于"日期"列中的每个日期(月和年(,查看"结束"列中具有相同月和年的所有行,如果月和年匹配,则将冻结设置为TRUE。
即,最终结果应该是这样的,
id date start end product result
10 2022-06-10 2022-02-10 2022-06-12 183 TRUE
10 2022-06-11 2022-02-10 2022-06-12 183 TRUE
10 2022-06-12 2022-02-10 2022-06-12 183 TRUE
10 2022-06-13 null null 183 TRUE
10 2022-06-14 null null 183 TRUE
......
10 2022-07-01 null null 183 FALSE
如有任何帮助,我们将不胜感激。
请考虑以下bigquery方法。
select *,
case
when date is not null and
end is not null and
date_trunc(date,month) = date_trunc(end,month)
then "TRUE"
else "FALSE"
end as New_Result
from Data
我添加了一个null检查,如果日期或结束都有null值,则返回错误结果,但如果需要,可以将其分离以标记这些实例,如下所示。
select *,
case
when date is null or end is null then "CHECK ME"
when date_trunc(date,month) = date_trunc(end,month) then "TRUE"
else "FALSE"
end as New_Result,
from Data
查询时使用别名怎么样:
SELECT id, date, start, end, productid, (date=end) as result FROM ProductTable;
因此,当您进行查询时,您将得到最后一列date=end的布尔结果。