BigQuery比较DATE和TIMESTAMP

时间:2019-01-17 15:15:26

标签: google-bigquery

这是我在MySQL中使用的示例。但是,在BigQuery中,我的OnSite timestamp DATE ,而我的Documents时间戳是 TIMESTAMP

BigQuery遇到以下查询时遇到问题,因为我收到了消息:

  

对于参数类型DATE,函数DATE没有匹配的签名。支持的签名:DATE(TIMESTAMP,[STRING]); DATE(DATETIME); DATE(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         |

View on DB Fiddle

3 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

StationIds

如果要应用于问题的样本数据

#standardSQL
SELECT
  IFNULL(OnSite.worksite_id, Documents.worksite_id) AS `Worksite`,
  IFNULL(OnSite.timestamp, DATE(Documents.timestamp)) AS `DATE`,
  COUNT(Documents.worksite_id) AS `Users_on_Site`,
  COUNT(DISTINCT OnSite.uid) AS `Completed`
FROM `project.dataset.OnSite` OnSite
LEFT JOIN `project.dataset.Documents` Documents 
ON OnSite.worksite_id = Documents.worksite_id 
AND OnSite.timestamp = DATE(Documents.timestamp)
GROUP BY `DATE`, `Worksite`

结果将符合预期

WITH `project.dataset.OnSite` AS (
  SELECT "u12345" uid, "worksite_1" worksite_id, DATE '2019-01-01' `TIMESTAMP` UNION ALL
  SELECT "u12345", "worksite_1", '2019-01-02' UNION ALL
  SELECT "u12345", "worksite_1", '2019-01-03' UNION ALL
  SELECT "u12345", "worksite_1", '2019-01-04' UNION ALL
  SELECT "u12345", "worksite_1", '2019-01-05' UNION ALL
  SELECT "u12345", "worksite_1", '2019-01-06' UNION ALL
  SELECT "u1", "worksite_1", '2019-01-01' UNION ALL
  SELECT "u1", "worksite_1", '2019-01-02' UNION ALL
  SELECT "u1", "worksite_1", '2019-01-05' UNION ALL
  SELECT "u1", "worksite_1", '2019-01-06' 
), `project.dataset.Documents` AS (
  SELECT "1" document_id,     "u12345" uid,   "worksite_1" worksite_id, 'work_permit' type,    TIMESTAMP '2019-01-01 00:00:00' `TIMESTAMP` UNION ALL
  SELECT "2",     "u12345",   "worksite_2", 'job',            '2019-01-02 00:00:00' UNION ALL
  SELECT "3",     "u12345",   "worksite_1", 'work_permit',    '2019-01-03 00:00:00' UNION ALL
  SELECT "4",     "u12345",   "worksite_2", 'job',            '2019-01-04 00:00:00' UNION ALL
  SELECT "5",     "u12345",   "worksite_1", 'work_permit',    '2019-01-05 00:00:00' UNION ALL
  SELECT "6",     "u12345",   "worksite_2", 'job',            '2019-01-06 00:00:00' UNION ALL
  SELECT "7",     "u12345",   "worksite_1", 'work_permit',    '2019-01-07 00:00:00' UNION ALL
  SELECT "8",     "u12345",   "worksite_2", 'work_permit',    '2019-01-09 00:00:00' UNION ALL
  SELECT "9",     "u12345",   "worksite_1", 'job',            '2019-01-09 00:00:00' UNION ALL
  SELECT "10",    "u12345",   "worksite_2", 'work_permit',    '2019-01-09 00:00:00' UNION ALL
  SELECT "11",    "u12345",   "worksite_1", 'work_permit',    '2019-01-09 00:00:00' UNION ALL
  SELECT "12",    "u12345",   "worksite_2", 'work_permit',    '2019-01-09 00:00:00' UNION ALL
  SELECT "13",    "u12345",   "worksite_1", 'job',            '2019-01-09 00:00:00' UNION ALL
  SELECT "14",    "u12345",   "worksite_2", 'work_permit',    '2019-01-09 00:00:00' UNION ALL
  SELECT "15",    "u12345",   "worksite_1", 'work_permit',    '2019-01-09 00:00:00' 
)

答案 1 :(得分:0)

BigQuery documentation中,解释了DATE(year, month, day)函数接受以下输入:

  
      
  1. DATE(timestamp_expression[, timezone]):根据表示年,月和日的INT64值构造一个DATE。

  2.   
  3. DATE:将timestamp_expression转换为DATE数据类型。它支持一个可选参数来指定时区。如果未指定时区,则使用默认时区UTC。

  4.   

在您的用例中,您传递给DATETIME_TRUNC的值似乎已经是一个日期时间。为此,您可以使用DATETIME_TRUNC(IFNULL(OnSite.timestamp, Documents.timestamp), DAY) ,例如:

Private Sub Form_Load()
   Dim clmX As ColumnHeader
   Dim itmX As ListItem
   Dim i As Integer

   For i = 1 To 3
      Set clmX = ListView1.ColumnHeaders.Add()
      clmX.Text = "Column " & i
   Next i

   ' Inclui 10 items com o mesmo ícone
   For i = 1 To 10
      Set itmX = ListView1.ListItems.Add()
      itmX.SmallIcon = 1
      itmX.Text = "ListItem " & i
      itmX.SubItems(1) = "Subitem 1"
      itmX.SubItems(2) = "Subitem 2"
      If i = 2 Then
      'here
       'itmX.removeCheckbox
      End If
   Next i
End Sub

答案 2 :(得分:0)

您为什么不强行铸造一切并使生活更轻松:-)?所有这些都应该起作用:

select 
   date(timestamp('2019-01-02')), 
   date(timestamp('2019-01-02 00:00:00')), 
   date(timestamp(null)))

因此,在您的if null语句中:

SELECT
  IFNULL(OnSite.worksite_id, Documents.worksite_id) as `Worksite`,
  IFNULL(date(datetime(OnSite.timestamp)),date(datetime(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(datetime(OnSite.timestamp)) = DATE(datetime(Documents.timestamp))
GROUP BY `Date`, `Worksite`;