Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 01:05 PM
Hello Richie,
In Databricks, you can use a combination of NULLIF and COALESCE functions to handle divide-by-zero scenarios effectively. Here's an example of how you can modify your percentage calculation:
SELECT
MetricNo,
MetricName,
Amount,
COALESCE(
(Amount / NULLIF(TotalAmount, 0)) * 100,
0
) AS Percentage
FROM YourFinancialMetricsView
This approach works as follows:
NULLIF(TotalAmount, 0)returns NULL if TotalAmount is zero, avoiding the divide-by-zero error- If the division results in NULL (due to a zero denominator),
COALESCEwill return 0 as the percentage