C
cgsanders7
I am trying to write a function that returns a table representing all assets
for a given deal aggregated on the issue level of any date. The function
tf_Asset_Portfolio(@deal_id, @as_of_date) should return the following fields:
issue_id
par_amount - total_par_amount for an issue
market_value - total calculated market value for an issue; market value
calculated as follows: for non-defaulted issues (default_date is not
specified) - 100% of par_amount, for issues that defaulted less than one year
ago - 65% of par_amount, for issues that are one or more years in default - 0.
The table layout is as follows:
Assets-
deal_id
issue_id
par_amount
Issue-
issue_id
issuer_id
default_date
Issuer_Rating_History
issuer_id
as_of_date
Market value isn't a field in any table but the return table and that is the
field I'm trying to do the calculation on. Anyone have an idea about how to
do this because everything I try isn't working. I can return a table without
calculating the field or I can return a calculation on one field but I dont
know how to tie both of these together. Here are my examples that cant do
both. Thanks in advance.
This one can return the table without the calculation -
CREATE FUNCTION [dbo].[tf_Asset_Portfolio](@deal_id int,
@as_of_date datetime)
RETURNS TABLE
AS
RETURN ( SELECT dbo.Assets.issue_id, SUM(DISTINCT dbo.Assets.par_amount) AS
par_amount, SUM(DISTINCT dbo.Assets.par_amount) AS market_value
FROM dbo.Issue INNER JOIN
dbo.Assets ON dbo.Issue.issue_id = dbo.Assets.issue_id
INNER JOIN
dbo.Issuer_Rating_History ON dbo.Issue.issuer_id = dbo.
Issuer_Rating_History.issuer_id
WHERE (dbo.Issuer_Rating_History.as_of_date <= @as_of_date)
GROUP BY ALL dbo.Assets.issue_id, dbo.Assets.deal_id, dbo.Issue.default_date
HAVING (dbo.Assets.deal_id = @deal_id) )
This one can do the calculation -
CREATE FUNCTION dbo.tf_Asset_Portfolio2
(@deal_id int,
@as_of_date datetime)
RETURNS @Market TABLE
(issue_id int, par_amount money, market_value money)
AS
BEGIN
DECLARE @ReturnDate datetime
DECLARE @DD datetime
DECLARE @PA money
DECLARE @MV money
DECLARE @ID int
DECLARE @DateD int
SELECT TOP 1
@ReturnDate = LAST_BATCH
FROM master..sysprocesses
WHERE SPId = @@SPID
SELECT @ID = issue_id FROM Assets WHERE Assets.deal_id = @deal_id
SELECT @PA = SUM(DISTINCT par_amount) FROM Assets WHERE Assets.issue_id = @ID
AND Assets.deal_id = @deal_id
SELECT @DD = default_date FROM Issue WHERE Issue.issue_id = @ID
SET @DateD = DateDiff("yyyy", @DD, @ReturnDate)
If @DD = Null
BEGIN
SET @MV = @PA
END
Else If @DD > @ReturnDate
BEGIN
SET @MV = @PA
END
Else If @DateD < 1
BEGIN
SET @MV = @PA * .65
END
Else If @DateD >= 1
BEGIN
SET @MV = 0
END
insert into @Market
(issue_id, par_amount, market_value)
values
(@ID,@PA,@MV)
RETURN
END
Please help, I think I'm going in the wrong direction.
for a given deal aggregated on the issue level of any date. The function
tf_Asset_Portfolio(@deal_id, @as_of_date) should return the following fields:
issue_id
par_amount - total_par_amount for an issue
market_value - total calculated market value for an issue; market value
calculated as follows: for non-defaulted issues (default_date is not
specified) - 100% of par_amount, for issues that defaulted less than one year
ago - 65% of par_amount, for issues that are one or more years in default - 0.
The table layout is as follows:
Assets-
deal_id
issue_id
par_amount
Issue-
issue_id
issuer_id
default_date
Issuer_Rating_History
issuer_id
as_of_date
Market value isn't a field in any table but the return table and that is the
field I'm trying to do the calculation on. Anyone have an idea about how to
do this because everything I try isn't working. I can return a table without
calculating the field or I can return a calculation on one field but I dont
know how to tie both of these together. Here are my examples that cant do
both. Thanks in advance.
This one can return the table without the calculation -
CREATE FUNCTION [dbo].[tf_Asset_Portfolio](@deal_id int,
@as_of_date datetime)
RETURNS TABLE
AS
RETURN ( SELECT dbo.Assets.issue_id, SUM(DISTINCT dbo.Assets.par_amount) AS
par_amount, SUM(DISTINCT dbo.Assets.par_amount) AS market_value
FROM dbo.Issue INNER JOIN
dbo.Assets ON dbo.Issue.issue_id = dbo.Assets.issue_id
INNER JOIN
dbo.Issuer_Rating_History ON dbo.Issue.issuer_id = dbo.
Issuer_Rating_History.issuer_id
WHERE (dbo.Issuer_Rating_History.as_of_date <= @as_of_date)
GROUP BY ALL dbo.Assets.issue_id, dbo.Assets.deal_id, dbo.Issue.default_date
HAVING (dbo.Assets.deal_id = @deal_id) )
This one can do the calculation -
CREATE FUNCTION dbo.tf_Asset_Portfolio2
(@deal_id int,
@as_of_date datetime)
RETURNS @Market TABLE
(issue_id int, par_amount money, market_value money)
AS
BEGIN
DECLARE @ReturnDate datetime
DECLARE @DD datetime
DECLARE @PA money
DECLARE @MV money
DECLARE @ID int
DECLARE @DateD int
SELECT TOP 1
@ReturnDate = LAST_BATCH
FROM master..sysprocesses
WHERE SPId = @@SPID
SELECT @ID = issue_id FROM Assets WHERE Assets.deal_id = @deal_id
SELECT @PA = SUM(DISTINCT par_amount) FROM Assets WHERE Assets.issue_id = @ID
AND Assets.deal_id = @deal_id
SELECT @DD = default_date FROM Issue WHERE Issue.issue_id = @ID
SET @DateD = DateDiff("yyyy", @DD, @ReturnDate)
If @DD = Null
BEGIN
SET @MV = @PA
END
Else If @DD > @ReturnDate
BEGIN
SET @MV = @PA
END
Else If @DateD < 1
BEGIN
SET @MV = @PA * .65
END
Else If @DateD >= 1
BEGIN
SET @MV = 0
END
insert into @Market
(issue_id, par_amount, market_value)
values
(@ID,@PA,@MV)
RETURN
END
Please help, I think I'm going in the wrong direction.