使用不同的日期格式在 Big Query 中查询多个表
Querying multiple tables in Big Query with different dateformat
我对使用 BigQuery 和编写查询还很陌生
我想从 2 个不同的 table 中提取数据,每个都有一个带有日期戳但格式不同的字段。所有字段都以纯文本形式导入,因此我必须将每个字段转换为其他格式。
Table 1 在日期字段中的格式为“2020-10-09 00:00:00”,Table 2 的格式为“2020-10-09”
这是我使用的代码
SELECT
e.Region,
e.OmradeNamn,
SAFE_CAST(e.Datum as Date) as Datum,
FORMAT_DATE('%u', SAFE_CAST(e.Datum as Date)) AS Veckodag,
e.Rutt_Stilling,
...
...
...
...
r.NA_ArbPass,
FROM `leveranskvalitet` e
LEFT JOIN `rutterap` r
ON e.Rutt_Stilling = r.Rutt_Stilling
and e.Datum = r.Datum
Where cast(LEFT(e.Datum,10) as date) = Cast(@DATUM as date)
...
...
Order by e.Rutt_Stilling ASC>
从 table 2 它只得到 'NULL'
如果我使用日期格式为“2020-10-09”的另一个 table 作为 table 1,我可以毫无问题地从 table 2
中提取数据
到 select 日期我使用来自另一个 sheet
的单元格引用 (@Datum)
感谢您对此提供的帮助
罗杰
假设数据类型是时间戳和日期,您可以执行以下操作:
with temp_ts as (
select timestamp('2020-10-09 00:00:00') as ts
),
temp_dt as (
select date('2020-10-09') as dt
)
select *
from temp_ts t
join temp_dt d
on extract(date from t.ts) = d.dt;
鉴于您的查询:
SELECT
e.Region,
e.OmradeNamn,
SAFE_CAST(e.Datum as Date) as Datum,
FORMAT_DATE('%u', SAFE_CAST(e.Datum as Date)) AS Veckodag,
e.Rutt_Stilling,
...
...
...
...
r.NA_ArbPass,
FROM `leveranskvalitet` e
LEFT JOIN `rutterap` r
ON e.Rutt_Stilling = r.Rutt_Stilling
and date(cast(e.Datum as datetime) = cast(r.Datum as date)
Where cast(LEFT(e.Datum,10) as date) = Cast(@DATUM as date)
...
...
Order by e.Rutt_Stilling ASC>
我对使用 BigQuery 和编写查询还很陌生
我想从 2 个不同的 table 中提取数据,每个都有一个带有日期戳但格式不同的字段。所有字段都以纯文本形式导入,因此我必须将每个字段转换为其他格式。
Table 1 在日期字段中的格式为“2020-10-09 00:00:00”,Table 2 的格式为“2020-10-09” 这是我使用的代码
SELECT
e.Region,
e.OmradeNamn,
SAFE_CAST(e.Datum as Date) as Datum,
FORMAT_DATE('%u', SAFE_CAST(e.Datum as Date)) AS Veckodag,
e.Rutt_Stilling,
...
...
...
...
r.NA_ArbPass,
FROM `leveranskvalitet` e
LEFT JOIN `rutterap` r
ON e.Rutt_Stilling = r.Rutt_Stilling
and e.Datum = r.Datum
Where cast(LEFT(e.Datum,10) as date) = Cast(@DATUM as date)
...
...
Order by e.Rutt_Stilling ASC>
从 table 2 它只得到 'NULL'
如果我使用日期格式为“2020-10-09”的另一个 table 作为 table 1,我可以毫无问题地从 table 2
中提取数据到 select 日期我使用来自另一个 sheet
的单元格引用 (@Datum)感谢您对此提供的帮助 罗杰
假设数据类型是时间戳和日期,您可以执行以下操作:
with temp_ts as (
select timestamp('2020-10-09 00:00:00') as ts
),
temp_dt as (
select date('2020-10-09') as dt
)
select *
from temp_ts t
join temp_dt d
on extract(date from t.ts) = d.dt;
鉴于您的查询:
SELECT
e.Region,
e.OmradeNamn,
SAFE_CAST(e.Datum as Date) as Datum,
FORMAT_DATE('%u', SAFE_CAST(e.Datum as Date)) AS Veckodag,
e.Rutt_Stilling,
...
...
...
...
r.NA_ArbPass,
FROM `leveranskvalitet` e
LEFT JOIN `rutterap` r
ON e.Rutt_Stilling = r.Rutt_Stilling
and date(cast(e.Datum as datetime) = cast(r.Datum as date)
Where cast(LEFT(e.Datum,10) as date) = Cast(@DATUM as date)
...
...
Order by e.Rutt_Stilling ASC>