CASE 句
ADQL クエリに条件を追加するには、SELECT 句内の CASE 句を使用します。特定の条件でデータを変換できます。この句は、集約関数とバケット関数をサポートします。
構文
SELECT CASE WHEN (condition) THEN (action 1) ELSE (action 2) END FROM event_type
SELECT CASE WHEN (condition 1) THEN (action 1)
WHEN (condition 2) THEN (action 2)
WHEN (condition 3) THEN (action 3)
ELSE (action 4) END FROM event_type
メトリック関数の使用例
集約には CASE 句のメトリック関数を使用できます。次に計数と平均集約関数を使用した例を示します。
アプリケーションのユーザー体験に基づくトランザクションの計数
SELECT application, CASE WHEN userExperience = "NORMAL" THEN count(userExperience) AS normalTransactionsCount ELSE count(userExperience) AS otherTransactionsCount END FROM transactions
アプリケーションnormalTransactionsCountotherTransactionsCount
ユーザー体験の値とアプリケーションの合計トランザクション数を表示する
SELECT application, CASE WHEN userExperience = "NORMAL" THEN userExperience AS userExperienceValues ELSE userExperience AS otherUserExperienceValues END, count(userExperience) AS totalTransactionsCount FROM transactions
null と表示されます。
アプリケーションの通常のトランザクションおよびその他のトランザクションの数の表示
SELECT application, transactionName, CASE WHEN userExperience = "NORMAL" THEN count(userExperience) AS normalTransactionsCount ELSE count(userExperience) AS otherTransactionsCount END FROM transactions
アプリケーショントランザクション名normalTransactionsCountotherTransactionsCount
各ユーザー体験値の数の表示
SELECT count(*), userExperience, CASE WHEN userExperience = "NORMAL" THEN count(userExperience) AS normalTransactionsCount ELSE count(userExperience) AS otherTransactionsCount END FROM transactions
応答時間が長いアプリケーションのトランザクション数の表示
SELECT responseTime, CASE WHEN userExperience = "NORMAL" THEN count(userExperience) AS normalTransactionsCount ELSE count(userExperience) AS otherTransactionsCount END FROM transactions WHERE responseTime > 10
応答時間normalTransactionsCountotherTransactionsCount
ユーザー体験トランザクションの平均応答時間の計算
SELECT application, CASE WHEN userExperience="NORMAL" THEN avg(responseTime) AS normalTransactionsAvgResponseTime WHEN userExperience="SLOW" THEN avg(responseTime) AS slowTransactionsAvgResponseTime ELSE avg(responseTime) AS otherTransactionsAvgResponseTime END FROM transactions
アプリケーションnormalTransactionsAvgResponseTimeslowTransactionsAvgResponseTimeotherTransactionsAvgResponseTime
メトリック関数なしでの使用例
次に、メトリック関数を使用しない例を示します。
応答時間でユーザー体験の値を表示する
SELECT CASE WHEN userExperience = "NORMAL" THEN userExperience AS userExperienceValues ELSE userExperience AS otherUserExperienceValues END, responseTime FROM transactions
null と表示されます。
userExperienceValuesotherUserExperienceValues応答時間
応答時間が NULL ではないユーザー体験値を表示する
SELECT CASE WHEN userExperience = "NORMAL" THEN userExperience AS userExperienceValues ELSE userExperience AS otherUserExperienceValues END, responseTime FROM transactions WHERE userExperience IS NOT NULL
null と表示されます。
userExperienceValuesotherUserExperienceValues応答時間
CASE 句の後のステートメント
CASE 句は何らかの条件の下というロジックを適用して特定のメトリックを計算するもので、句の外部で定義された別のメトリックをネストできません。以下に有効な ADQL クエリを示します。結果は指定した順序で表示されます。
SELECT application,
CASE
WHEN userExperience = "NORMAL" THEN count(userExperience) AS normalTransactionsCount
ELSE count(userExperience) AS otherTransactionsCount
END,
count(*)
FROM transactions
- アプリケーション
- normalTransactionCount
- otherTransactionCount
- count
SELECT application,
count(*),
CASE
WHEN userExperience = "NORMAL" THEN count(userExperience) AS normalTransactionsCount
ELSE count(userExperience) AS otherTransactionsCount
END
FROM transactions
- アプリケーション
- count
- normalTransactionCount
- otherTransactionCount
- CASE ブロックは
normalTransactionsCountやotherTransactionsCountなどの、特定のメトリックを計算する条件を定めるロジックに使用されます。 count(*)は、アプリケーションごとにグループ化されたすべてのトランザクションの合計数を反映する、別のメトリックです。
制限事項
-
フィールドに算術演算を使用しません。PYTHON
SELECT CASE WHEN userExperience = "NORMAL" THEN (responseTime+10) AS normalResponseTimesModified ELSE (responseTime-10) AS otherResponseTimesModified END from transactions -
count、sum、averageなどのメトリック式を算術演算に使用しません。PYTHONSELECT CASE WHEN userExperience = "NORMAL" THEN (count(userExperience)+10) AS normalTxnCountModified ELSE (count(userExperience)-10) AS otherTxnCountModified END from transactions -
CASE 句の後の非メトリック派生フィールドでは算術演算を使用しません。PYTHON
SELECT CASE WHEN userExperience = "NORMAL" THEN responseTime AS normalResponseTimeValues ELSE responseTime AS otherResponseTimeValues END, normalResponseTimeValues+5, otherResponseTimeValues-10 from transactions