SELECT
b.Product_Code,
b.Account_Id,
b.Channel_Desc,
r.Min_Range, r.Max_Range,
b.Balance AS openingbalance,
CASE
WHEN b.Balance > r.Max_Range
THEN r.Max_Range - r.Min_Range
ELSE b.Balance - r.Min_Range
END AS Split_Balance,
(CASE
WHEN b.Balance > r.Max_Range THEN r.Max_Range - r.Min_Range
ELSE b.Balance - r.Min_Range
END) / balance AS weighted_Avg
FROM
[EDH_DM_PRD].[dbo].[DataMart_Deposit] AS b
JOIN
[DBStats].[dbo].[Split_balances] AS r ON b.Product_Code = r.Product_Code
AND b.Channel_Desc = r.Channel_Desc
AND b.Asat_Dt = '20190131'
AND b.Acct_Status_Desc = 'Open'
AND b.Balance >= 0
AND b.Product_Code = '2000-0100'
AND b.Channel_Desc = 'Broker BDMs'
AND b.Account_Id = '31179111'
ORDER BY
Account_Id, Channel_Desc, Min_Range
Этот SQL-запрос возвращает этот набор результатов:
| Product_Code | Account_Id | Channel_Desc | Min_Range | Max_Range | openingbalance | Split_Balance | weighted_Avg |
| 2000-0100 | 31179111 | Broker BDMs | 0.000 | 4999.990 | 122314.480 | 4999.990 | 0.04087815277471645221 |
| 2000-0100 | 31179111 | Broker BDMs | 5000.000 | 9999.990 | 122314.480 | 4999.990 | 0.04087815277471645221 |
| 2000-0100 | 31179111 | Broker BDMs | 10000.000 | 19999.990 | 122314.480 | 9999.990 | 0.08175638730590196679 |
| 2000-0100 | 31179111 | Broker BDMs | 20000.000 | 49999.990 | 122314.480 | 29999.990 | 0.24526932543064402513 |
| 2000-0100 | 31179111 | Broker BDMs | 50000.000 | 99999999999.990 | 122314.480 | 72314.480 | 0.59121765468814485414 |
В результирующем наборе «начальный баланс» следует указывать только в первом ряду.
Остальные строки должны показывать «openbalance» как 0.