大于等于逻辑异常:SQL区间分组边界值未被正确归类问题
Let's break down what's going wrong and how to fix it quickly:
The Root Problem
Looking at your two UPDATE statements:
UPDATE sb_actprod_reins.psold_ho_mapping_trended SET EG = 14 WHERE iexpos2 <=75 AND iexpos2 > 70; UPDATE sb_actprod_reins.psold_ho_mapping_trended SET EG = 15 WHERE iexpos2 <=80 AND iexpos > 75;
You have a typo in the second statement—you used iexpos instead of iexpos2 for the lower boundary check. That means values like 75.1 are never matching the second condition (since it's checking a different field entirely), so they get left out of both groups.
Fixed SQL Statements
First, correct the typo in the second query:
UPDATE sb_actprod_reins.psold_ho_mapping_trended SET EG = 14 WHERE iexpos2 <=75 AND iexpos2 > 70; UPDATE sb_actprod_reins.psold_ho_mapping_trended SET EG = 15 WHERE iexpos2 <=80 AND iexpos2 > 75;
This will properly catch values between 75 and 80 (exclusive of 75, inclusive of 80) and assign EG=15.
A More Efficient Alternative: Use a CASE Statement
Instead of running two separate UPDATE queries (which scans the table twice), you can handle all assignments in a single query with CASE. This is cleaner and better for performance:
UPDATE sb_actprod_reins.psold_ho_mapping_trended SET EG = CASE WHEN iexpos2 > 70 AND iexpos2 <=75 THEN 14 WHEN iexpos2 >75 AND iexpos2 <=80 THEN 15 -- Add more conditions here if you have additional groups ELSE EG -- Leave existing EG values unchanged for rows outside these ranges END;
Key Notes for Interval Grouping
- Always double-check field names to avoid typos—they're one of the most common causes of "missing" data issues.
- Make sure your intervals are contiguous (no gaps or overlaps). In your case, the first group ends at 75, and the second starts just above 75, so there's no gap between them once the typo is fixed.
- Using
CASEensures you only process the table once, which is especially helpful if you're working with large datasets.
内容的提问来源于stack exchange,提问作者JoeJam

