4

Wondering if anyone has suggestions on implementing a case or IF statement with U-SQL. For example, how do i convert this:

SELECT
    FirstName, LastName,
    Salary, DOB,
    CASE Gender
        WHEN 'M' THEN 'Male'
        WHEN 'F' THEN 'Female'
    END
FROM Employees;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
enufeffizy
  • 121
  • 2
  • 5

6 Answers6

5

You can use an inline C# expression for simple things like so (did not test it yet though)

SELECT
    FirstName, LastName,
    Salary, DOB,
    Gender == "Male" ? "M" : "F"
FROM Employees

If it is more complex consider writing a user defined operator in C#.
Have a look at the MSDN tutorial here

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
3

An example of using ?: in lieu of CASE is available at the official U-SQL Language Reference site under Common SQL Expressions in U-SQL.

ravuya
  • 8,586
  • 4
  • 30
  • 33
2

U-SQL supports the ANSI CASE expression as per the release notes here, since Spring 2018. A simple example:

DECLARE @outputFile string = @"\output\output.csv";

@Employees =
    SELECT *
    FROM (
        VALUES
        ( "w", "Bob", 1999, "31/12/1999", "M" ),
        ( "Sheena", "Easton", 1999, "31/12/1998", "F" )
        ) AS Employees(FirstName, LastName, Salary, dob, Gender);


@output =
    SELECT FirstName,
           LastName,
           Salary,
           dob,
           CASE Gender
               WHEN "M" THEN "Male"
               WHEN "F" THEN "Female"
           END AS Gender
    FROM @Employees;


OUTPUT @output
TO @outputFile
USING Outputters.Csv(quoting : false);
wBob
  • 13,710
  • 3
  • 20
  • 37
1

The solution above is correct. But I'd prefer to use IF instead inline C# code.

SELECT
    FirstName, LastName,
    Salary, DOB,
    IF(Gender == "Male", "M", "F") AS Gender
FROM Employees
Dmitry Petrov
  • 1,490
  • 1
  • 19
  • 34
1

SELECT
FirstName, LastName,
Salary, DOB,
(Gender == 'M') ? 'Male'
(Gender == 'F') ? 'Female' AS Gender

FROM Employees;

This will Work on your Case

Bala cse
  • 119
  • 1
  • 2
0

As other answers did not provide the exact equivalent, I am providing the same below.

The corresponding expression in U-SQL is. We have to use conditional expression (?:). For more information, you can refer to below links:

C# Expressions for ?:
TSQL and USQL equivalents

SELECT
    FirstName, LastName,
    Salary, DOB,
    (Gender == "M") ? "Male" : (Gender == "F") ? "Female" : "Unknown" AS Gender
FROM Employees;
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58