Finding minimum value across different columns in SAP HANA

In SAP HANA, when you need to find the minimum value across multiple columns in a single row, you should use the LEAST function instead of the MIN function. The MIN function is an aggregate function that works across rows, while LEAST compares values across columns within the same row.

Using LEAST Function

The LEAST function syntax allows you to compare multiple column values and returns the smallest value among them ?

SELECT ID, LEAST(DAY_1, DAY_2, DAY_3) AS MIN_VALUE 
FROM your_table_name;

Example

Consider a table with daily sales data across three days. Here's how to find the minimum sales value for each record ?

SELECT 
    CUSTOMER_ID, 
    LEAST(DAY_1_SALES, DAY_2_SALES, DAY_3_SALES) AS MIN_DAILY_SALES
FROM SALES_DATA
WHERE CUSTOMER_ID IN ('C001', 'C002', 'C003');

The output of the above query would be ?

CUSTOMER_ID    MIN_DAILY_SALES
C001           150.00
C002           200.50  
C003           175.25

Important Considerations

NULL Value Handling: Please make sure that none of the values in your columns are NULL. If any column contains NULL values, the LEAST function will return NULL for that row. To handle NULL values, you can use the COALESCE function to provide default values ?

SELECT ID, 
       LEAST(COALESCE(DAY_1, 0), 
             COALESCE(DAY_2, 0), 
             COALESCE(DAY_3, 0)) AS MIN_VALUE
FROM your_table_name;

Conclusion

The LEAST function is the correct choice for finding minimum values across columns in SAP HANA, providing row-wise comparison rather than aggregate operations across multiple rows.

Updated on: 2026-03-13T18:19:12+05:30

720 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements