Mengganti kode pembayaran dengan nama yang mudah dikenali:
CASE Payment Type
WHEN "CC" THEN "Credit Card"
WHEN "DC" THEN "Debit Card"
WHEN "GC" THEN "Gift Card"
WHEN "CA" THEN "Cash"
ELSE "Other"
END
Sintaks
CASE input_expression
WHEN expression_to_match THEN result
[WHEN expression_to_match THEN result]
[...]
[ELSE else_result]
END
Parameter
input_expression - Kolom atau ekspresi yang valid.
[expression_to_match - Kolom atau ekspresi yang valid. Klausa WHEN membandingkan input_expression dengan input_expression dan menampilkan benar jika keduanya sama, atau salah jika tidak sama.]{#when-conditions}
result - Kolom atau ekspresi yang valid. Setiap klausa WHEN harus memiliki klausa THEN yang cocok, yang menentukan hasil yang akan ditampilkan jika kondisi tersebut benar. Jika ada beberapa klausa WHEN, pernyataan CASE akan menampilkan hasil untuk klausa benar yang pertama.
else_result (opsional) - Kolom atau ekspresi yang valid. Klausa else_resultELSE menentukan hasil default untuk pernyataan CASE. Klausa ini ditampilkan jika tidak ada klausa WHEN yang benar. Jika pernyataan CASE tidak memiliki klausa ELSE, dan tidak ada klausa WHEN yang benar, pernyataan CASE akan menampilkan NULL.
Cara kerja CASE sederhana
Pernyataan CASE sederhana terdiri dari elemen berikut:
Kata kunci CASE, diikuti dengan ekspresi input.
WHEN : nilai yang akan dibandingkan dengan input_expression : jika nilai sama dengan input_expression, berarti klausa ini benar. Anda dapat memiliki beberapa klausa WHEN dalam satu pernyataan CASE.
THEN : hasil yang akan ditampilkan jika kondisi klausa WHEN benar. Anda harus memiliki satu klausa THEN untuk setiap klausa WHEN dalam pernyataan CASE.
ELSE : Opsional. Jika tidak ada kondisi klausa WHEN yang benar, CASE akan menampilkan nilai dalam klausa ELSE, atau NULL jika tidak ada klausa ELSE yang ditentukan.
Kata kunci END.
CASE mengevaluasi setiap klausa WHEN yang berurutan dan menampilkan hasil pertama yang kondisinya benar. Klausa WHEN yang tersisa dan hasil ELSE tidak akan dievaluasi. Jika semua kondisi WHEN bernilai salah atau NULL, CASE akan menampilkan hasil ELSE, atau jika tidak ada klausa ELSE, NULL akan ditampilkan.
Contoh
Memberikan link yang disesuaikan untuk pelanggan premium Anda:
CASE Premium Status
WHEN "Platinum" THEN CONCAT(Site URL, "platinum_welcome.html")
WHEN "Gold" THEN CONCAT(Site URL, "gold_welcome.html")
WHEN "Silver" THEN CONCAT(Site URL, "silver_welcome.html")
ELSE CONCAT(Site URL, "welcome.html")
END
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-03 UTC."],[],[],null,["CASE (simple)\n=============\n\nSimple `CASE` returns a result based on the value of a single input expression, or a default result if none of the comparison values match.\n| **Note:** There are two forms of the `CASE` statement: *searched* `CASE` and *simple* `CASE`. Searched `CASE` statements allow you to use more sophisticated logic, while simple `CASE` statements are simpler to construct.\n\nSee also: [IF](/looker/docs/studio/if).\n\nSample usage\n------------\n\nReplace payment codes with intuitive names: \n\n```\n CASE Payment Type\n WHEN \"CC\" THEN \"Credit Card\"\n WHEN \"DC\" THEN \"Debit Card\"\n WHEN \"GC\" THEN \"Gift Card\"\n WHEN \"CA\" THEN \"Cash\"\n ELSE \"Other\"\n END\n```\n\nSyntax\n------\n\n```\n CASE input_expression\n WHEN expression_to_match THEN result\n [WHEN expression_to_match THEN result]\n [...]\n [ELSE else_result]\n END\n```\n\n### Parameters\n\n- \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e - Any valid field or expression.\n- \\[\u003cvar translate=\"no\"\u003eexpression_to_match\u003c/var\u003e - Any valid field or expression. The `WHEN` clause compares \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e to \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e and returns true if the two are equal, or false if they aren't.\\]{#when-conditions}\n- \u003cvar translate=\"no\"\u003eresult \u003c/var\u003e - Any valid field or expression. Each `WHEN` clause must have a matching `THEN` clause, which specifies the results to return if that condition is true. If there are multiple `WHEN` clauses, the `CASE` statement returns the result for the first true clause.\n- \u003cvar translate=\"no\"\u003eelse_result\u003c/var\u003e (optional) - Any valid field or expression. The `ELSE` *else_result* clause specifies a default result for the `CASE` statement. This clause is returned if none of the `WHEN` clauses are true. If a `CASE` statement has no `ELSE` clause, and none of the `WHEN` clauses are true, the `CASE` statement returns `NULL`.\n\n| **Note:** All of the `THEN` clauses in a `CASE` statement must return the same type of result. For example, if the first `THEN` clause returns the **Text** data type, additional `THEN` clauses must also return the **Text** data type.\n\nHow simple `CASE` works\n-----------------------\n\nA simple `CASE` statement consists of the following elements:\n\n- The `CASE` keyword, followed by an input expression.\n- `WHEN` : the value against which to compare the \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e : if the value equals the \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e, then this clause is true. You can have multiple `WHEN` clauses in a single `CASE` statement.\n- `THEN` : the result to return if the `WHEN` clause's condition is true. You must have one `THEN` clause for each `WHEN` clause in your `CASE` statement.\n- `ELSE` : Optional. If none of the `WHEN` clause conditions are true, `CASE` returns the value in the `ELSE` clause, or `NULL` if no `ELSE` clause is specified.\n- The `END` keyword.\n\n`CASE` evaluates each successive `WHEN` clause and returns the first result where the condition is true. Any remaining `WHEN` clauses and the `ELSE` result are not evaluated. If all `WHEN` conditions are false or `NULL`, `CASE` returns the `ELSE` result, or if no `ELSE` clause is present, returns `NULL`.\n\nExample\n-------\n\nProvide customized links for your premium customers: \n\n```\n CASE Premium Status\n WHEN \"Platinum\" THEN CONCAT(Site URL, \"platinum_welcome.html\")\n WHEN \"Gold\" THEN CONCAT(Site URL, \"gold_welcome.html\")\n WHEN \"Silver\" THEN CONCAT(Site URL, \"silver_welcome.html\")\n ELSE CONCAT(Site URL, \"welcome.html\")\n END\n```\n\nRelated resources\n-----------------\n\n- [Calculated fields](/looker/docs/studio/about-calculated-fields)\n- [Looker Studio function list](/looker/docs/studio/function-list)"]]