这是我在 MySQL 中使用的示例。但是,在 BigQuery 中,我的 OnSitetimestamp
是DATE而我的 Documents 时间戳是TIMESTAMP。
BigQuery 在以下查询中遇到问题,因为我收到以下消息:
参数类型的函数 DATE 没有匹配的签名:DATE。支持的签名:DATE(TIMESTAMP, [STRING]);日期(日期时间);日期(INT64,INT64,INT64)在 [8:146]
有谁知道我需要做什么才能使查询与比较 DATE 和 TIMESTAMP 一起工作?
架构(MySQL v5.7)
CREATE TABLE OnSite
(`uid` varchar(55), `worksite_id` varchar(55), `timestamp` datetime)
;
INSERT INTO OnSite
(`uid`, `worksite_id`, `timestamp`)
VALUES
("u12345", "worksite_1", '2019-01-01'),
("u12345", "worksite_1", '2019-01-02'),
("u12345", "worksite_1", '2019-01-03'),
("u12345", "worksite_1", '2019-01-04'),
("u12345", "worksite_1", '2019-01-05'),
("u12345", "worksite_1", '2019-01-06'),
("u1", "worksite_1", '2019-01-01'),
("u1", "worksite_1", '2019-01-02'),
("u1", "worksite_1", '2019-01-05'),
("u1", "worksite_1", '2019-01-06')
;
CREATE TABLE Documents
(`document_id` varchar(55), `uid` varchar(55), `worksite_id` varchar(55), `type` varchar(55), `timestamp` datetime)
;
INSERT INTO Documents
(`document_id`, `uid`, `worksite_id`, `type`, `timestamp`)
VALUES
("1", "u12345", "worksite_1", 'work_permit', '2019-01-01 00:00:00'),
("2", "u12345", "worksite_2", 'job', '2019-01-02 00:00:00'),
("3", "u12345", "worksite_1", 'work_permit', '2019-01-03 00:00:00'),
("4", "u12345", "worksite_2", 'job', '2019-01-04 00:00:00'),
("5", "u12345", "worksite_1", 'work_permit', '2019-01-05 00:00:00'),
("6", "u12345", "worksite_2", 'job', '2019-01-06 00:00:00'),
("7", "u12345", "worksite_1", 'work_permit', '2019-01-07 00:00:00'),
("8", "u12345", "worksite_2", 'work_permit', '2019-01-09 00:00:00'),
("9", "u12345", "worksite_1", 'job', '2019-01-09 00:00:00'),
("10", "u12345", "worksite_2", 'work_permit', '2019-01-09 00:00:00'),
("11", "u12345", "worksite_1", 'work_permit', '2019-01-09 00:00:00'),
("12", "u12345", "worksite_2", 'work_permit', '2019-01-09 00:00:00'),
("13", "u12345", "worksite_1", 'job', '2019-01-09 00:00:00'),
("14", "u12345", "worksite_2", 'work_permit', '2019-01-09 00:00:00'),
("15", "u12345", "worksite_1", 'work_permit', '2019-01-09 00:00:00')
;
查询 #1
SELECT
IFNULL(OnSite.worksite_id, Documents.worksite_id) as `Worksite`,
DATE(IFNULL(OnSite.timestamp, Documents.timestamp)) as `Date`,
COUNT(Documents.worksite_id) as `Users_on_Site`,
COUNT(DISTINCT OnSite.uid) as `Completed`
FROM OnSite
LEFT JOIN Documents ON OnSite.worksite_id = Documents.worksite_id AND DATE(OnSite.timestamp) = DATE(Documents.timestamp)
GROUP BY `Date`, `Worksite`;
| Worksite | Date | Users_on_Site | Completed |
| ---------- | ---------- | ------------- | --------- |
| worksite_1 | 2019-01-01 | 2 | 2 |
| worksite_1 | 2019-01-02 | 0 | 2 |
| worksite_1 | 2019-01-03 | 1 | 1 |
| worksite_1 | 2019-01-04 | 0 | 1 |
| worksite_1 | 2019-01-05 | 2 | 2 |
| worksite_1 | 2019-01-06 | 0 | 2 |