我需要从旧版迁移到标准SQL 此查询:
SELECT MAX(FECHA)
FROM(
SELECT FECHA, DAYOFWEEK(FECHA) AS DIA
FROM(
SELECT DATE(DATE_ADD(TIMESTAMP("2017-05-29"), pos - 1, "DAY")) AS FECHA
FROM (
SELECT ROW_NUMBER() OVER() AS pos, *
FROM (
FLATTEN((
SELECT SPLIT(RPAD('', 1 + DATEDIFF(TIMESTAMP(CURRENT_DATE()),
TIMESTAMP("2017-05-29")), '.'),'') AS h
FROM (SELECT NULL)),h
)))
))
WHERE DIA=1
查询必须从当前日期返回上一个最接近的星期日。
当我在标准SQL中运行时,我得到
语法错误:预期的关键字JOIN但在[12:2]得到“)”(在FROM(SELECT NULL)之后),h
答案 0 :(得分:2)
查询必须从当前日期返回上一个最接近的星期日。
#standardSQL
SELECT
DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY)
您可以将CURRENT_DATE()
替换为任何日期,它将返回上一个最接近的星期日
答案 1 :(得分:2)
您可以将DATE_TRUNC
与WEEK
部分一起使用,以截断到最近的星期日。例如:
#standardSQL
WITH Input AS (
SELECT date
FROM UNNEST([
DATE '2017-06-26',
DATE '2017-06-24',
DATE '2017-05-04']) AS date
)
SELECT
date,
FORMAT_DATE('%A', date) AS dayofweek,
DATE_TRUNC(date, WEEK) AS previous_sunday
FROM Input;
返回:
+------------+-----------+-----------------+
| date | dayofweek | previous_sunday |
+------------+-----------+-----------------+
| 2017-06-24 | Saturday | 2017-06-18 |
| 2017-05-04 | Thursday | 2017-04-30 |
| 2017-06-26 | Monday | 2017-06-25 |
+------------+-----------+-----------------+