aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sql/serenitasdb.sql18
1 files changed, 12 insertions, 6 deletions
diff --git a/sql/serenitasdb.sql b/sql/serenitasdb.sql
index ac0b6014..a1a6921f 100644
--- a/sql/serenitasdb.sql
+++ b/sql/serenitasdb.sql
@@ -638,15 +638,16 @@ CREATE OR REPLACE FUNCTION get_tranche_quotes(pg_index_type text, pg_series inte
pg_date date) RETURNS SETOF tranche_quotes AS
$$
DECLARE r RECORD;
+ DECLARE best_quote RECORD DEFAULT NULL;
DECLARE lower_attach smallint;
DECLARE flag boolean;
BEGIN
IF lower(pg_index_type) ='hy' AND pg_series in (9, 10)
THEN
- lower_attach = 10::smallint;
+ lower_attach := 10::smallint;
ELSE
- lower_attach = 0::smallint;
+ lower_attach := 0::smallint;
END IF;
flag := FALSE;
FOR r in EXECUTE
@@ -669,18 +670,23 @@ $$
AND quotedate=$4 AND detach-attach!=5::smallint AND quotesource=$5'
INTO flag
USING pg_index_type, pg_series, pg_tenor, r.quotedate, r.quotesource, lower_attach;
+ IF flag THEN
+ best_quote := r;
+ END IF;
EXIT WHEN r.quotesource != 'CITI' AND flag;
-
END LOOP;
+ IF best_quote IS NULL THEN
+ RETURN;
+ END IF;
IF pg_index_type = 'HY' AND pg_series >=15 THEN
RETURN QUERY
SELECT * FROM tranche_quotes WHERE index=pg_index_type::index_type AND series= pg_series
- AND tenor=pg_tenor::tenor AND quotedate=r.quotedate AND detach-attach!=5::smallint AND
- quotesource=r.quotesource ORDER BY attach ASC;
+ AND tenor=pg_tenor::tenor AND quotedate=best_quote.quotedate AND detach-attach!=5::smallint AND
+ quotesource=best_quote.quotesource ORDER BY attach ASC;
ELSE
RETURN QUERY
SELECT * FROM tranche_quotes WHERE index=pg_index_type::index_type AND series=pg_series
- AND tenor=pg_tenor::tenor AND quotedate=r.quotedate AND quotesource=r.quotesource
+ AND tenor=pg_tenor::tenor AND quotedate=best_quote.quotedate AND quotesource=best_quote.quotesource
ORDER BY attach asc;
END IF;
END;