create table DepositRates (
id int not null,
month int not null,
rate decimal(6,6) null,
resetDate date null,
resetNote nvarchar(50) null);
create table Instruments (
id int not null,
name nvarchar(8) not null,
source nvarchar(8) not null);
insert into DepositRates values(1,1,0.008000,to_date('10/02/2019','MM/DD/YYYY'),'late reset');
insert into DepositRates values(1,2,0.025500,to_date('11/01/2019','MM/DD/YYYY'),NULL);
insert into DepositRates values(1,3,0.014100,to_date('11/29/2019','MM/DD/YYYY'),'early reset');
insert into DepositRates values(2,1,0.007115,to_date('10/01/2019','MM/DD/YYYY'),NULL);
insert into DepositRates values(2,2,0.007159,to_date('11/01/2019','MM/DD/YYYY'),NULL);
insert into DepositRates values(2,3,0.007135,to_date('12/01/2019','MM/DD/YYYY'),NULL);
insert into DepositRates values(3,1,0.022441,to_date('10/01/2019','MM/DD/YYYY'),NULL);
insert into DepositRates values(3,2,0.020109,NULL,'missing reset');
insert into DepositRates values(3,3,0.017743,to_date('12/01/2019','MM/DD/YYYY'),NULL);
insert into Instruments values(1,'HKDOND=','Reuters');
insert into Instruments values(2,'GBP3MD=','Euribor');
insert into Instruments values(3,'USD1MD=','Reuters');
select * from Instruments;
ID NAME SOURCE
-- ------- --------
1 HKCOMD= Reuters
2 GBP3MD= Euribor
3 USD1MD= Reuters
select * from DepositRates;
ID MONTH RATE RESETDATE RESETNOTE
-- ----- -------- ---------- ----------
1 1 0.008000 2019-10-02 late reset
1 2 0.025500 2019-11-01 NULL
1 3 0.014100 2019-11-29 early reset
2 1 0.007115 2019-10-01 NULL
2 2 0.007159 2019-11-01 NULL
2 3 0.007135 2019-12-01 NULL
3 1 0.022441 2019-10-01 NULL
3 2 0.020109 NULL missing reset
3 3 0.017743 2019-12-01 NULL
select
i.name "name",
i.source "source",
max(r.rate*(nullif((1-abs(sign(r.month-1))),0))) "current rate",
max(r.rate*(nullif((1-abs(sign(r.month-2))),0))) "period-1 rate",
max(r.rate*(nullif((1-abs(sign(r.month-3))),0))) "period-2 rate"
from DepositRates r, Instruments i
where r.id = i.id
group by i.name,i.source
order by i.name;
name source current rate period-1 rate period-2 rate
------- ------- ------------ ------------- -------------
GBP3MD= Euribor 0.007115 0.007159 0.007135
HKDOND= Reuters 0.008000 0.025500 0.014100
USD1MD= Reuters 0.022441 0.020109 0.017743
select
id,
nullif((1-abs(sign(month-1))),0) as v1,
nullif((1-abs(sign(month-2))),0) as v2,
nullif((1-abs(sign(month-3))),0) as v3
from DepositRates
order by id,month;
ID V1 V2 V3
-- ---- ---- ----
1 1 NULL NULL
1 NULL 1 NULL
1 NULL NULL 1
2 1 NULL NULL
2 NULL 1 NULL
2 NULL NULL 1
3 1 NULL NULL
3 NULL 1 NULL
3 NULL NULL 1
select
id,
rate*nullif((1-abs(sign(month-1))),0) as v1,
rate*nullif((1-abs(sign(month-2))),0) as v2,
rate*nullif((1-abs(sign(month-3))),0) as v3
from DepositRates
order by id,month;
ID V1 V2 V3
-- -------- -------- --------
1 0.008000 NULL NULL
1 NULL 0.025500 NULL
1 NULL NULL 0.014100
2 0.007115 NULL NULL
2 NULL 0.007159 NULL
2 NULL NULL 0.007135
3 0.022441 NULL NULL
3 NULL 0.020109 NULL
3 NULL NULL 0.017743
select
i.name "name",
i.source "source",
max(add_days(r.resetDate,1-nullif((1-abs(sign(r.month-1))),0))) "current reset",
max(add_days(r.resetDate,1-nullif((1-abs(sign(r.month-2))),0))) "period-1 reset",
max(add_days(r.resetDate,1-nullif((1-abs(sign(r.month-3))),0))) "period-2 reset"
from DepositRates r, Instruments i
where r.id = i.id
group by i.name,i.source
order by i.name;
name source current reset period-1 reset period-2 reset
------- ------- ------------- -------------- --------------
GBP3MD= Euribor 2019-10-01 2019-11-01 2019-12-01
HKDOND= Reuters 2019-10-02 2019-11-01 2019-11-29
USD1MD= Reuters 2019-10-01 NULL 2019-12-01
select
i.name "name",
i.source "source",
max(substring(r.resetNote,nullif((1-abs(sign(r.month-1))),0),length(r.resetNote))) "current note",
max(substring(r.resetNote,nullif((1-abs(sign(r.month-2))),0),length(r.resetNote))) "period-1 note",
max(substring(r.resetNote,nullif((1-abs(sign(r.month-3))),0),length(r.resetNote))) "period-2 note"
from DepositRates r, Instruments i
where r.id = i.id
group by i.name,i.source
order by i.name;
name source current note period-1 note period-2 note
------- ------- ------------ ------------- -------------
GBP3MD= Euribor NULL NULL NULL
HKDOND= Reuters late reset NULL early reset
USD1MD= Reuters NULL missing reset NULL
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |