decode1
Function
The decode1 function implements if-then-else branch selection functionality.
Syntax
decode1(expression, search, result [, search, result]...[, default])
Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| expression | Yes | All data types | The expression to be compared. |
| search | Yes | Consistent with expression | The search term to compare with the expression. |
| result | Yes | All data types | The return value when the search and expression values match. |
| default | No | Consistent with result | The default value returned if no search term matches. If not specified, NULL is returned. |
Return Values
result and default are the return values, supporting all data types.
- If a match is found, result is returned.
- If no match is found, default is returned.
- If default is not specified, NULL is returned.
- If search options are duplicated and a match occurs, the first value is returned.
Example Code
To facilitate understanding of the function's usage, source data is provided along with function-related examples. Create the salary table and add data with the following example command:
CREATE EXTERNAL TABLE IF NOT EXISTS salary ( dept_id STRING, -- <department-ID> userid STRING, -- <employee-ID> sal INT -- <salary> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION 'obs://your-bucket-name/aidatalake/salary/';-- [Mandatory] Replace with OBS bucket path (required when using external tables on the platform)
Add data as follows:
INSERT INTO salary VALUES
('d1','user1',1000),
('d1','user2',2000),
('d1','user3',3000),
('d2','user4',4000),
('d2','user5',5000),
('d3','user6',6000),
('d4','user7',7000),
('d5','user8',8000); Command Output
Return the name for each department.
When the value of dept_id is d1, return Platform. When the value is d2, return MRS. For all other cases, return Others.
select dept_id, decode1(dept_id, 'd1', 'Platform', 'd2', 'MRS', 'Others') as result from salary GROUP BY dept_id; -- Deduplicate by department ID, ensuring only one row per department is returned
Result:
+-----+-------+ | dept_id| result| +-----+-------+ | d1| Platform| | d2| MRS| | d3| Others| | d4| Others| | d5| Others| +-----+-------+
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot