如何在WHERE子句中聚合尾随的12个月(TTM)总使用CASE语句?谷歌-大量查询
这是我的查询...
SELECT
clientid,
clientname,
year(revrecdate)*100 + month(revrecdate) as Period, *** NAMES the TTM period
count(revrecdate) as Jobs,
sum(profit) as Profits
FROM
oiafd.JobProfit
WHERE
revrecdate >= '2010-12-01 00:00:00'
and clientid = '2KOOLPDX'
and CASE WHEN month(revrecdate) = 1
THEN(revrecdate <= timestamp(concat(string(year(revrecdate)),'-01-31')) and
revrecdate >= timestamp(concat(string(year(revrecdate)-1),'-02-01')))
WHEN month(revrecdate) = 2
THEN(revrecdate <= timestamp(concat(string(year(revrecdate)),'-02-28')) and
revrecdate >= timestamp(concat(string(year(revrecdate)-1),'-03-01')))
答案 0 :(得分:1)
回答有关将CASE语句的概念转换为适用于BigQuery的问题的问题:您可以使用IF函数将每个案例分解为WHERE中的单独子句。
这样的事情会起作用:
...
WHERE
revrecdate >= '2010-12-01 00:00:00'
and clientid = '2KOOLPDX'
and IF(month(revrecdate) = 1,
revrecdate <= timestamp(concat(string(year(revrecdate)),'-01-31')) and
revrecdate >= timestamp(concat(string(year(revrecdate)-1),'-02-01')),
true)
and IF(month(revrecdate) = 2,
revrecdate <= timestamp(concat(string(year(revrecdate)),'-02-28')) and
revrecdate >= timestamp(concat(string(year(revrecdate)-1),'-03-01')),
true)
有关IF()的详细信息,请参阅https://developers.google.com/bigquery/query-reference#otherfunctions。