大查询-日期比较

时间:2018-09-06 15:44:02

标签: google-bigquery

我正在尝试从我在BigQuery中创建的表中检索该表,该表具有许多列,其中之一是Date。

类型为DATE,我只想检索两个日期之间的记录。

请在下面查看我的代码。

SELECT ExpakAccounts.Account As Account1, ExpakAccounts.description As Name, collection.Col_Dpt As Col_Dpt, collection.Own_Dpt As Own_Dpt, collection.Service As Service, COUNT(collection.Parcels) AS Cons, SUM(collection.Amount) AS Amount, collection.Filename As Filename, collection.Date AS Date 
FROM `rawdataset.ExPakAccounts2` AS ExpakAccounts 
INNER JOIN `rawdataset.collection_2018` AS Collection ON ExpakAccounts.Account = collection.Account
WHERE ExpakAccounts.Account = collection.Account AND collection.Cat = 100 AND (Col_Dpt <> Own_Dpt) AND (collection.Service = 32 OR collection.Service = 68) AND (DATE(Date) >= '2018-07-30' AND DATE(Date) <= '2018-08-06')
GROUP BY Account1, Name, Col_Dpt, Own_Dpt, Service, Cat, Filename, Date;

错误消息

No matching signature for function DATE for argument types: DATE

1 个答案:

答案 0 :(得分:2)

您可以在Date BETWEEN '2018-07-30' AND '2018-08-06子句中使用WHERE。这将返回日期在2018-07-30和2018-08-06之间的行。

此外,如果您的字段Date已经是类型DATE,并且使用DATE(Date)将引发以下错误No matching signature for function DATE for argument types: DATE. Supported signatures: DATE(TIMESTAMP, [STRING]); DATE(DATETIME); DATE(INT64, INT64, INT64),因为函数日期仅支持TIMESTAMPINTEGER

相反,您将在where子句中按原样使用Date字段(不使用DATE函数)。

如果您的Date字段是字符串类型,则可以在CAST(Date AS DATE) BETWEEN '2018-07-30' AND '2018-08-06子句中使用WHERE

Here是有关BigQuery中标准SQL函数的更多信息