correct the below code which should validate in sap datasphere case when days_between(LWEDT, CURRENT_DATE) >= 0 and days_between(LWEDT, CURRENT_DATE) < 61 Then '0 to 60 Days' when days_between(LWEDT, CURRENT_DATE) >= 61 and days_between(LWEDT, CURREN
The provided SQL case statement seems to have overlapping ranges and redundant conditions, which could lead to logically incorrect outcomes. Here’s a corrected version that ensures proper conditions without redundancy:
```sql
CASE
WHEN days_between(LWEDT, CURRENT_DATE) < 0 THEN 'Non-Batch Managed'
WHEN days_between(LWEDT, CURRENT_DATE) < 61 THEN '0 to 60 Days'
WHEN days_between(LWEDT, CURRENT_DATE) < 91 THEN '61 to 90 Days'
WHEN days_between(LWEDT, CURRENT_DATE) < 181 THEN '3 to 6 Months'
WHEN days_between(LWEDT, CURRENT_DATE) < 365 THEN '6 to 12 Months'
WHEN days_between(LWEDT, CURRENT_DATE) < 545 THEN '12 to 18 Months'
WHEN days_between(LWEDT, CURRENT_DATE) < 731 THEN '18 to 24 Months'
WHEN days_between(LWEDT, CURRENT_DATE) >= 731 THEN '> 24 Months'
ELSE 'Total Stock in Hand'
END
```
### Explanation of Changes:
1. **Removed Redundant Conditions**: The last few conditions in the original code overlap with earlier conditions, which can lead to confusion.
2. **Placed a `Non-Batch Managed` Check First**: I moved the 'Non-Batch Managed' condition to the beginning to handle negative day differences first.
3. **Simplified Ranges**: Subsequent ranges only specify upper limits, allowing each range to logically follow from the previous one without needing to check for the lower bound again.
This version should function properly for its purpose in SAP Datasphere.