如何检查日期是否与另一列中的日期在同一个月内 SQL BQ

发布于 01-19 22:39 字数 969 浏览 4 评论 0 原文

我的BQ表中有两个日期字段。一个是 start_date ,另一个是 end_date 。这两个列的数据类型是: date 。数据看起来像这样:

start_date           end_date
2022-02-28           2022-02-28
2022-02-28           2022-03-01
2022-03-01           2022-03-02
2022-03-01           2022-04-01

我需要创建一个新列,并检查两个日期是否在同个月内。值将是“是”和“否”。是的,如果两个日期在同个月内,则否。 这是所需的输出:

start_date           end_date              outcome
2022-02-28           2022-02-28              yes
2022-02-28           2022-03-01              no
2022-03-01           2022-03-02              yes
2022-03-01           2022-04-01              no

我尝试了:

select
    case
        when (DATE(start_date)) = (
            DATE(end_date),
            INTERVAL 1 MONTH
        )
    ) then 'yes'
    else 'no'
end as outcome
FROM
    my_bq_table

遇到一个错误: ocerator = for Crignt类型没有匹配的签名:日期,struct< date,Interval>。支持的签名:任何=任何

I have two date fields in my BQ table. One is start_date and the other one is end_date. DataType for both columns is: DATE. Data looks like this:

start_date           end_date
2022-02-28           2022-02-28
2022-02-28           2022-03-01
2022-03-01           2022-03-02
2022-03-01           2022-04-01

I need to create a new column and check if both dates are within the same month. The values would be "yes" and "no". Yes if two dates are within the same month, no if not.
This is desired output:

start_date           end_date              outcome
2022-02-28           2022-02-28              yes
2022-02-28           2022-03-01              no
2022-03-01           2022-03-02              yes
2022-03-01           2022-04-01              no

I tried:

select
    case
        when (DATE(start_date)) = (
            DATE(end_date),
            INTERVAL 1 MONTH
        )
    ) then 'yes'
    else 'no'
end as outcome
FROM
    my_bq_table

Got an error: No matching signature for operator = for argument types: DATE, STRUCT<DATE, INTERVAL>. Supported signature: ANY = ANY

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2025-01-26 22:39:10

我建议使用“提取”来完成此任务。请参阅下面。

架构(MySQL V8.0)

CREATE TABLE Date_Table (
  `start_date` DATE,
  `end_date` DATE
);

INSERT INTO Date_Table
  (`start_date`, `end_date`)
VALUES
  ('2022-02-28', '2022-02-28'),
  ('2022-02-28', '2022-03-01'),
  ('2022-03-01', '2022-03-02'),
  ('2022-03-01', '2022-04-01');

查询#1

select *,
case when extract(month from start_date) = extract(month from end_date) then "Yes" else "No" End as Same_Month 
from Date_Table;
start_date end_date same_month
2022-02-28 2022-02-28 YES
2022-02-28 2022-28 2022-28 2022-03-03
-03-01 2022-03-02
2022-03-01 2022-04-01

<="" a="">

I'd propose using 'extract' to accomplish this task. Please see below.

Schema (MySQL v8.0)

CREATE TABLE Date_Table (
  `start_date` DATE,
  `end_date` DATE
);

INSERT INTO Date_Table
  (`start_date`, `end_date`)
VALUES
  ('2022-02-28', '2022-02-28'),
  ('2022-02-28', '2022-03-01'),
  ('2022-03-01', '2022-03-02'),
  ('2022-03-01', '2022-04-01');

Query #1

select *,
case when extract(month from start_date) = extract(month from end_date) then "Yes" else "No" End as Same_Month 
from Date_Table;
start_date end_date Same_Month
2022-02-28 2022-02-28 Yes
2022-02-28 2022-03-01 No
2022-03-01 2022-03-02 Yes
2022-03-01 2022-04-01 No

View on DB Fiddle

放肆 2025-01-26 22:39:10

日期(DATE_TRUNC(开始日期,月份)

DATE(DATE_TRUNC(start_date, MONTH)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文