我有一个包含数据的表,其中一列标题为'createdAt',是一个时间戳。是否有一个我可以运行的查询,它会选择上一周内所有的条目?
这是我到目前为止的代码。我相信它会实现某种WHERE子句,但我不确定如何做到这一点。
#standardSQL
SELECT
Serial,
SUM(ConnectionTime/3600) as Total_Hours,
COUNT(DISTINCT DeviceID) AS Devices_Connected
FROM `dataworks-356fa.FirebaseArchive.testf`
WHERE Model = "BlueBox-pH"
GROUP BY Serial
ORDER BY Serial
LIMIT 1000;
答案 0 :(得分:2)
在标准SQL中,您可以尝试这样的操作来查看WHERE子句是否为您提供了正确的日期范围:
SELECT
MIN(createdAt),
MAX(createdAt)
FROM
`dataworks-356fa.FirebaseArchive.testf`
WHERE
EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1
请注意,BigQuery使用星期日作为一周的第一天。我不知道如何改变它。如果有人知道,在我的国家,我们会认为星期一是本周的第一天,这会很有趣。
答案 1 :(得分:0)
您可以将DATE_TRUNC
与WEEK
部分一起使用,以查找给定日期的周开始日期。例如,
#standardSQL
WITH Input AS (
SELECT DATE '2017-06-25' AS date, 1 AS x UNION ALL
SELECT DATE '2017-06-20', 2 UNION ALL
SELECT DATE '2017-06-26', 3 UNION ALL
SELECT DATE '2017-07-11', 4 UNION ALL
SELECT DATE '2017-07-09', 5
)
SELECT
DATE_TRUNC(date, WEEK) AS week,
MAX(x) AS max_x
FROM Input
GROUP BY week;
在您的特定情况下,它将是:
#standardSQL
SELECT
Serial,
SUM(ConnectionTime/3600) as Total_Hours,
COUNT(DISTINCT DeviceID) AS Devices_Connected
FROM `dataworks-356fa.FirebaseArchive.testf`
WHERE Model = "BlueBox-pH" AND
createdAt >= DATE_TRUNC(CURRENT_DATE(), WEEK)
GROUP BY Serial
ORDER BY Serial
LIMIT 1000;
或者,如果您只是在过去七天内查找日期,则可以使用此表单的查询:
#standardSQL
SELECT
Serial,
SUM(ConnectionTime/3600) as Total_Hours,
COUNT(DISTINCT DeviceID) AS Devices_Connected
FROM `dataworks-356fa.FirebaseArchive.testf`
WHERE Model = "BlueBox-pH" AND
createdAt >= DATE_SUB(CURRENT_DATE(), INTERVAL 1 WEEK)
GROUP BY Serial
ORDER BY Serial
LIMIT 1000;
答案 2 :(得分:0)
选择上一周内所有的参赛作品?
下面是针对BigQuery Standard SQL并将数据限制在前一周,显然不是当前的一周
#standardSQL
SELECT
Serial,
SUM(ConnectionTime/3600) AS Total_Hours,
COUNT(DISTINCT DeviceID) AS Devices_Connected
FROM `dataworks-356fa.FirebaseArchive.testf`,
UNNEST([DATE_SUB(CURRENT_DATE(), INTERVAL CAST(FORMAT_DATE('%w', CURRENT_DATE()) AS INT64) DAY)]) AS first_day_of_week
WHERE Model = 'BlueBox-pH'
AND createdAt
BETWEEN DATE_SUB(first_day_of_week, INTERVAL 7 DAY)
AND DATE_SUB(first_day_of_week, INTERVAL 1 DAY)
GROUP BY Serial
-- ORDER BY Serial
-- LIMIT 1000
了解past week
它的工作原理 - 在
#standardSQL
WITH dates AS (
SELECT createdAt
FROM UNNEST(GENERATE_DATE_ARRAY('2017-01-01', '2017-01-13', INTERVAL 1 DAY)) AS createdAt
)
SELECT
createdAt,
FORMAT_DATE('%a', createdAt) AS weekday,
FORMAT_DATE('%U', createdAt) AS week_start_Sunday,
FORMAT_DATE('%W', createdAt) AS week_start_Monday,
FORMAT_DATE('%V', createdAt) AS week_start_Monday_prorated,
DATE_SUB(createdAt, INTERVAL weekday_num DAY) AS first_day_of_week_Sunday,
DATE_SUB(createdAt, INTERVAL weekday_num - 1 DAY) AS first_day_of_week_Monday,
DATE_SUB(DATE_SUB(createdAt, INTERVAL weekday_num DAY), INTERVAL 7 DAY) AS first_day_of_prev_week_Sunday,
DATE_SUB(DATE_SUB(createdAt, INTERVAL weekday_num - 1 DAY), INTERVAL 7 DAY) AS first_day_of_prev_week_Monday
FROM dates, UNNEST([CAST(FORMAT_DATE('%w', createdAt) AS INT64)]) AS weekday_num
ORDER BY createdAt
输出是 -
createdAt weekday week_ week_ week_ first_day_ first_day_ first_day_ first_day_
start_ start_ start_ of_week_ of_week_ of_prev_week_ of_prev_week_
Sunday Monday Monday_ Sunday Monday Sunday Monday
prorated
---------------------------------------------------------------------------------------------------
2017-01-01 Sun 01 00 52 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-02 Mon 01 01 01 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-03 Tue 01 01 01 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-04 Wed 01 01 01 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-05 Thu 01 01 01 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-06 Fri 01 01 01 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-07 Sat 01 01 01 2017-01-01 2017-01-02 2016-12-25 2016-12-26
2017-01-08 Sun 02 01 01 2017-01-08 2017-01-09 2017-01-01 2017-01-02
2017-01-09 Mon 02 02 02 2017-01-08 2017-01-09 2017-01-01 2017-01-02
2017-01-10 Tue 02 02 02 2017-01-08 2017-01-09 2017-01-01 2017-01-02
2017-01-11 Wed 02 02 02 2017-01-08 2017-01-09 2017-01-01 2017-01-02
2017-01-12 Thu 02 02 02 2017-01-08 2017-01-09 2017-01-01 2017-01-02
2017-01-13 Fri 02 02 02 2017-01-08 2017-01-09 2017-01-01 2017-01-02
正如您所看到的,在我的回答中,我使用first_day_of_week_Sunday
的逻辑来计算first_day_of_week
如果您在我的国家/地区与@Wouter - 有相同的要求,我们认为星期一是一周的第一天 - 您可以使用first_day_of_week_Monday
的逻辑