Spark SQL Conditional Function

  • Last update: January 16, 2025
  • Overview

    This document introduces the syntax of conditional functions in Spark SQL.

    IF

    You are advised to use the IF function in New Calculation Column of FineDatalink.

    For details about the example, see Adding a Column Using the IF Function.

    NVL

    Syntax: NVL(Expression, Default value)

    Returns Default value if the value of Expression is null or the value of Expression otherwise.

    Example:

    In the source table, Ship Name contains null values, as shown in the following figure.

    1.png


    Use Spark SQL. If the Ship Name value is null, return Unknown. Otherwise, return the existing value, as shown in the following figure.

    select $[DB Table Input].`Order ID` ,nvl($[DB Table Input].`Ship Name`,'Unknown') from $[DB Table Input]

    2.png

    The result is shown in the following figure.

    3.png

    COALESCE

    COALESCE (Expression 1, Expression 2, ...): Returns the first non-null value, or null if all the values are null.

    Example:

    In the source table, both Ship City and Ship Region have null values, as shown in the following figure.

    4.png

    Use the COALESCE function in Spark SQL to return the first non-null value, as shown in the following figure.

    select $[File Input].`Ship City`, $[File Input].`Ship Region`,coalesce($[File Input].`Ship City` ,$[File Input].`Ship Region` ) from $[File Input]

    5.png

    The result is shown in the following figure.

    6.png

    CASE

    You are advised to use the SWITCH function in New Calculation Column of FineDataLink.

    For details about the example, see Adding a Column Using the SWITCH Function.

    Null Value Processing

    In FineDataLink, you can use Data Filtering to process the null values.

    isnull(Expression): Returns true if the value of Expression is null. Otherwise, return false.

    Operations in Data Filtering are shown in the following figure.

    7.png

    is not null(Expression): Return true if the value of Expression is not NULL, otherwise, return false.

    Operations in Data Filtering are shown in the following figure.

    8.png


    附件列表


    主题: Data Development - Scheduled Task
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy