0

最後の 12 か月 (TTM) の合計を集計するには、WHERE 句で CASE ステートメントを使用する方法を教えてください。Google BigQuery

これが私のクエリです...

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')))
4

1 に答える 1

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を参照してください。

于 2013-08-26T18:57:08.677 に答える